Why this chapter matters
Most analysis starts here. Reliable filtering and sorting prevent incorrect snapshots and reduce rework in dashboards.
What you will learn
- Select specific columns and aliases instead of using SELECT *.
- Filter rows with precise boolean logic in WHERE clauses.
- Sort output with ORDER BY using multiple sort keys.
Lessons in this chapter
- Query shape and result setsRead a PostgreSQL SELECT statement top to bottom and predict output columns and rows. Read the full guide →
- Filtering with WHEREApply comparison and logical operators to encode business conditions directly in SQL.
- Sorting and tie-breakingUse ORDER BY with ascending or descending rules and deterministic tie-break columns.
- Null-safe reporting basicsHandle NULL values in predicates so record counts and filters stay accurate.
Study task
Using a PostgreSQL orders table, return shipped orders from the previous calendar month and sort by shipped_at descending then order_id ascending.
Chapter checkpoint
In PostgreSQL, how do you list customer_id and total from orders where total is at least 100, sorted by total descending?
SELECT customer_id, total FROM orders WHERE total >= 100 ORDER BY total DESC;