SQL is not read in the order it is written. The clauses are evaluated
FROM, then WHERE, then GROUP BY, then HAVING, then SELECT, then ORDER BY, then
LIMIT, and almost every confusing error in this language follows from that one
fact. SELECT is sixth, which is why a column alias defined there cannot be used
in WHERE and can be used in ORDER BY.
Two consequences worth having by name. WHERE filters rows before any
grouping and HAVING filters groups after it, so a condition on a raw column
belongs in WHERE and a condition on a count belongs in HAVING. And LIMIT is
last, so LIMIT 10 without an ORDER BY returns ten arbitrary rows
that may differ between runs, which is not a bug in the database.
NULL is not a value and does not compare
Measured on MySQL 8.4 on 2026-09-02:SELECT (NULL = NULL) answers NULL, and so doesNULL <> NULL. Only NULL IS NULL answers 1. A
comparison against NULL is neither true nor false, and WHERE keeps only the rows
where the condition is true.
Three-valued SQL logic
Why WHERE maker_id <> 1 drops a NULL row
Comparing anything against NULL yields unknown rather than true or false, and WHERE keeps only rows where the predicate evaluates to true.
Predicate outcome (1 = true and kept, 0 = false and dropped, blank = NULL and dropped)
To keep missing rows alongside non-matching ones, write WHERE maker_id <> 1 OR maker_id IS NULL.
Interactive Practice Lab
Edit and run this snippet directly in your browser (zero setup)
CREATE TABLE workshop_orders (id INTEGER PRIMARY KEY, supplier TEXT, qty INTEGER, city TEXT);INSERT INTO workshop_orders VALUES (1, 'Sepehr', 15, 'Ardabil'), (2, 'Aria', 47, 'Tabriz'), (3, 'Sepehr', 22, 'Ardabil'), (4, 'Nima', 31, 'Tehran');SELECT supplier, COUNT(*) AS deliveries, SUM(qty) AS total_qtyFROM workshop_ordersGROUP BY supplierORDER BY total_qty DESC;Live Big-O Complexity & Binary Search Race Lab
Compare complexity curves at scale and watch Linear vs Binary Search race live.
Which produces the result people report as a bug. Four kilns, one with a null
maker: WHERE maker_id <> 1 answers 1 row, not 2. The null row
is not not-equal to 1, it is unknown, so it is dropped. Any filter that should
include the missing rows has to say so:WHERE maker_id <> 1 OR maker_id IS NULL.
Choosing columns, and the one to stop writing
SELECT *is fine at a prompt and wrong in code: it breaks when
a column is added, sends columns nobody reads, and prevents an index from
covering the query- An alias with
ASis the column name the application sees, so
naming it is part of the interface rather than decoration DISTINCTapplies to the whole row, not to the column it appears
next to, which is the most common misreading of itORDER BYcan use an alias or a position, and using the position
is how a query breaks silently when somebody adds a column
The other clause worth knowing early is LIMIT withOFFSET, because it is how every paged list is written and it gets
slower the further in you go: OFFSET 100000 makes the engine
produce and discard a hundred thousand rows before returning ten. Keyset paging,
carrying the last row's sort value and asking for rows after it, costs the same
at every page and is the fix once a list is long.
Columns, aliases, and paging
Query patterns that break vs contracts that hold
Small shortcuts in SELECT, DISTINCT, ORDER BY, and OFFSET become brittle as schemas and row counts grow.
- Fragile shortcuts4
- Breaks covering indexSELECT * in application code
- Breaks if column addedORDER BY column position
- Applies to whole rowAssuming DISTINCT targets one column
- Discards rows before readingLIMIT ... OFFSET 100000
- Durable habits4
- Enables index-only readsExplicit column list
- Clear app interfaceAS column alias
- Safe across schema editsORDER BY column or alias
- Steady cost per pageKeyset paging after last sort key
Keyset paging carries the last seen sort value instead of counting past a hundred thousand discarded rows.
What to run in 2026
EXPLAIN is the tool, free with every engine, and it belongs on
any query that will run more than occasionally. It reports whether the engine is
scanning a table or using an index, which is the difference this page cannot
show and the next one measures. It replaced timing a query, which tells you it
is slow and not why.
What to run in 2026
The iterative SQL workflow that replaces guesswork
Combining query history with EXPLAIN turns SQL editing into a repeatable check instead of retyping queries and guessing why they are slow.
Iterative query loop
- 01Recall from query history
- 02Adjust one clause
- 03Inspect with EXPLAIN
- 04Keep the proven query
- 01Recall from query historyStart from yesterday's query rather than retyping from a text file
- 02Adjust one clauseRefine columns, filters, or paging without transcription mistakes
- 03Inspect with EXPLAINSee table scan vs index use before trusting timing alone
- 04Keep the proven queryStore the working version for the next iteration
Timing tells you a query is slow; EXPLAIN tells you whether it scanned the table or used an index.
A database client with a query history is the second, free in several
versions, and the reason is the loop: writing SQL is iterative, and being able
to re-run yesterday's query with one change is most of the work. It replaced
retyping from a text file, which is where the transcription errors come from.
Where this stalls people
The stall is reading a query top to bottom and expecting it to execute that
way. Under that model, the alias in WHERE should work, LIMIT should apply before
ORDER BY, and HAVING and WHERE look interchangeable. The errors are then
arbitrary rules to memorise rather than consequences.
One query, seven clauses
The order the engine runs them, which is not the order you typed
Every rule about what may reference what is a consequence of this order, and reciting it is faster than remembering the rules.
- FROM and the joinsThe rows exist here. Nothing has been filtered yet
- WHEREFilters rows. A count does not exist yet, so it cannot be tested
- GROUP BYRows become groups. Individual columns stop being available
- HAVINGFilters groups. This is where a condition on a count belongs
- SELECTSixth. This is where an alias is created, which is why WHERE cannot use it
- ORDER BYAfter SELECT, so it can use an alias
- LIMITLast. Without an ORDER BY above it, the ten rows are arbitrary
The rail is the logical order rather than the physical one: an engine may reorder anything it likes as long as the answer matches this. What it may not change is which clause can see which name.
Learn the seven clause order once and every one of them becomes obvious.
Say it in order when a query surprises you, and the answer is nearly always in
which clause runs before which. The tables being read are
primary keys,
foreign keys and what they promise, combining more than one is
combining tables, and the four
kinds, and what happens after GROUP BY is
counting, summing and
grouping by, inside
the language for asking a database
questions.