Selecting and filtering data

The workhorse of SQL is the SELECT statement. Almost every analytics question starts here.

The shape of a query

SELECT order_id, customer_id, amount
FROM orders
WHERE amount > 100
ORDER BY amount DESC
LIMIT 5;

Reading order matters: FROM picks the table, WHERE filters rows, SELECT picks columns, ORDER BY sorts, LIMIT truncates.

Filtering patterns

-- multiple conditions
WHERE status = 'shipped' AND amount > 100

-- membership
WHERE country IN ('FR', 'DE', 'NL')

-- pattern matching
WHERE email LIKE '%@example.com'

-- null checks (never use = NULL)
WHERE shipped_at IS NOT NULL

Exercise: from an orders table, find the 10 most recent orders over €50 that have actually shipped.