A · QueryBeginner35 min read
3. WHERE, ORDER BY & LIMIT
SQL · 48 pages source format
Filter rows with WHERE. Sort with ORDER BY. Paginate with LIMIT/OFFSET. The bread and butter of exploratory queries.
What you'll learn
- WHERE conditions
- ORDER BY
- LIMIT/OFFSET
- AND/OR with parentheses
WHERE operators
=, !=, <, >, BETWEEN, IN, LIKE, IS NULL. AND/OR — use parentheses when mixing.
SELECT id, name, email
FROM users
WHERE active = 1
AND created_at >= '2024-01-01'
ORDER BY name ASC
LIMIT 10;Do this now
SQLBolt lessons 2–5. Save each to sql-notes/queries/.
Clear?
LIKE and NULL
LIKE 'A%' prefix match. IS NULL / IS NOT NULL — never = NULL (always unknown).
Do this now
Find users with NULL phone. Find emails ending in @company.com.
Clear?
Sort and paginate
ORDER BY created_at DESC, id ASC for tie-break. LIMIT 20 OFFSET 40 = page 3 at size 20.
Do this now
Query: newest 10 orders over $100. Second query: page 2 of active users.
Clear?