SQL Analysis · Chapter 1 of 10

SELECT, WHERE, and ORDER BY

Write clear PostgreSQL queries that pick needed columns, filter rows by business rules, and sort results for reporting.

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

  1. Query shape and result setsRead a PostgreSQL SELECT statement top to bottom and predict output columns and rows. Read the full guide →
  2. Filtering with WHEREApply comparison and logical operators to encode business conditions directly in SQL.
  3. Sorting and tie-breakingUse ORDER BY with ascending or descending rules and deterministic tie-break columns.
  4. 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;