You type a query top to bottom: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT. The engine logically evaluates it in a different order, and that order — not the typing order — explains almost everything that has ever surprised you in SQL.

Learn the logical order once, and a dozen apparently arbitrary rules become one rule seen from different angles.

One order that generates the rules

The logical order is stated in seven steps. First, FROM, including joins — the engine assembles the working set of rows. Second, WHERE — rows that do not satisfy the filter leave. Third, GROUP BY — the survivors are gathered into groups. Fourth, HAVING — groups that do not satisfy the group filter leave. Fifth, SELECT — the expressions and aliases in the select list are computed, and window functions are evaluated over what remains. Sixth, ORDER BY — the result is sorted. Seventh, LIMIT, or its equivalents — the sorted result is cut.

Write it on a card: FROM, WHERE, GROUP, HAVING, SELECT, ORDER, LIMIT. Every clause may only use what earlier clauses have produced. That single sentence is the concept.

The misconception is that the engine runs your query in the order you wrote it, starting with SELECT. It does not, and believing it does makes the language's rules look arbitrary — aliases that work in one clause and not another, aggregates banished from WHERE for no visible reason. Once the logical order is in place, none of those rules needs memorizing. Each one is the order, applied. If you find yourself memorizing exceptions, you are missing the one principle that generates them.

Watch the order answer three familiar puzzles

First, why can you not use a select-list alias in WHERE? Because WHERE is evaluated before SELECT computes the alias. At WHERE time the alias does not yet exist. Why can you use the alias in ORDER BY on most engines? Because ORDER BY comes after SELECT.

Second, why does WHERE salary > AVG(salary) fail? Because WHERE runs before grouping, when there is no group average in existence. The comparison belongs in HAVING, or in a subquery, or in a window function evaluated at SELECT time.

Third, why does a filter on an aggregate belong in HAVING rather than WHERE? For the same reason, stated positively: HAVING is the only filter that runs after groups exist. Row filters and group filters are not interchangeable labels. They run at different moments, on different objects.

Run the order against a concrete query

Consider this query: SELECT team, COUNT(*) AS headcount FROM staff WHERE salary >= 40000 GROUP BY team.

One further clause then filters the groups themselves — HAVING COUNT(*) >= 2 — and the result is ordered: ORDER BY headcount DESC.

Walk it clause by clause. FROM gives eight rows. WHERE keeps the six earning at least £40,000: Celia Vane, Jonas Reber, Farah Nasser, Ada Kowal, Felix Marlowe, and Dalia Mensah. Hugo Trent (£38,000) and Lena Vost (£36,000) leave now, before any grouping.

GROUP BY gathers the survivors: Management 1, Gardens 3, House 2. Shop has no survivors, so no Shop group is ever formed. HAVING keeps groups of two or more: Gardens and House. Management's single row leaves here, at group level, not row level. SELECT computes the counts. ORDER BY sorts: Gardens 3, then House 2.

The final result is exactly two rows — Gardens 3, House 2 — and you predicted every step of it without running anything. That prediction skill is the whole point. A query you can simulate in your head is a query you can debug, review, and defend.

The subtler consequences follow from the same order

DISTINCT, where used, applies after SELECT. It deduplicates computed rows, not source rows, which is why DISTINCT over a wide select list can be expensive and why it never fixes a join problem.

Window functions are evaluated at SELECT time, over rows that have already survived WHERE and grouping. That is why a window function sees the filtered world, and why you cannot filter on a window result in the same query's WHERE: the filter ran before the window existed. To filter on a window result you wrap the query — a derived table or a common table expression — so that the window result becomes an ordinary column of an outer query's FROM.

LIMIT runs last of all, which carries a warning worth making a rule. A LIMIT without an ORDER BY cuts an unsorted set, so which rows survive is the engine's choice, not yours, and may change between runs. If the cut matters, the sort must be stated.

Meaning first, cost second

Finally, hold a distinction the rest of the work depends on. The logical order is the language's contract: it defines what a query means. The physical order is the engine's business: it may read tables in any sequence, use any access path, and pipeline operations however it likes, provided the result matches the logical meaning.

Confusing the two is a category error in both directions. You cannot argue a result is right because the plan looked efficient, and you cannot argue a query is slow merely because its text is long. Query plans describe the physical side. Meaning first, cost second, each in its own vocabulary.

Once the order is in place, SQL stops presenting as a list of exceptions to memorize. It presents as one sequence, applied consistently, that a careful reader can simulate before the engine runs at all.

Experience it, don’t just read about it

The logical order FROM, WHERE, GROUP, HAVING, SELECT, ORDER, LIMIT defines what a query means is Concept 1 of Advanced SQL and Query Practice in Depth (Course No. 591), from Cognosco Learning, a learning company powered by AI. The course builds advanced SQL practice beyond this distinction — Joins at Depth — Multiplicity, Fanout, and the Row You Did Not Expect, Common Table Expressions — Naming the Steps of an Argument, and Reading a Query Plan — What the Engine Says It Will Do — each worked through with an Agentic Socratic AI coach, one learner at a time. Try a Free Preview at cognoscolearning.com.

Try a Free PreviewAll Signature CoursesThe Corporate License

Navigate your own course.

← All Insights

© 2026 Cognosco Learning. All rights reserved.