Logical Clause Order, Filtering & Pagination
SQL executes top-to-bottom in logical order — FROM first, then WHERE, SELECT, DISTINCT, ORDER BY, and LIMIT — regardless of how clauses are written.
Write SELECT first, but read execution starting at FROM. Aliases created in SELECT do not exist yet when WHERE runs, and NULL fails every equality check (= and !=) unless tested with IS NULL.Module 01 · Foundations · Reading Room
Mental Model
- 01.
FROM loads the source relation into working row memory before any column projection happens.
- 02.
WHERE tests every row independently against a three-valued predicate (TRUE, FALSE, UNKNOWN): only TRUE advances.
- 03.
AND binds tighter than OR, so compound filters without parentheses frequently admit unintended rows.
- 04.
ORDER BY and LIMIT run near the end of the pipeline, shaping a sequence that already survived filtering and projection.
Interactive Module Lab · Basics
2. Filtering with WHERE — sqlSELECT name, role, salary, countryFROM usersWHERE salary >= 85000ORDER BY salary DESC;
SOURCE: USERS
Scanned 10 rows from table 'users'.
| # | id | name | department_id | role | salary | state |
|---|---|---|---|---|---|---|
| 1 | 1 | Alice Smith | 1 | Software Engineer | 95,000 | keep |
| 2 | 2 | Bob Jones | 2 | Product Manager | 110,000 | keep |
| 3 | 3 | Charlie Lee | 1 | Software Engineer | 88,000 | keep |
| 4 | 4 | Diana Prince | 3 | UX Designer | 76,000 | keep |
| 5 | 5 | Evan Wright | 2 | Data Analyst | 82,000 | keep |
| 6 | 6 | Fiona Gallagher | 1 | Tech Lead | 135,000 | keep |
| 7 | 7 | George Clark | NULL | Intern | 42,000 | keep |
| 8 | 8 | Hannah Abbott | 2 | Scrum Master | 89,000 | keep |
Worked Presets in This Module
FROM reads the table first; SELECT only shapes the columns afterwards. Aliases are created at the very end.
WHERE tests every row and drops the ones that are not TRUE, then ORDER BY sorts what is left.
Without parentheses this means: USA, or (UK and salary over 100k). Watch which rows survive.
Three shortcuts for common tests: a list of values, an inclusive range, and a text pattern.
George has no department. "department_id != 1" is unknown for him, and WHERE only keeps TRUE, so he vanishes.