Stockertim Dictionary · Technology
Logical query order
Also called SQL order of execution, logical processing order, query execution order
In short
The sequence in which a database logically evaluates the clauses of an SQL query: FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, then LIMIT.
- Intermediate
- Concept
- Checked
In detail
SQL is written in one order and understood in another. A query is typed starting with SELECT, but logically the database begins with FROM and any joins, which define the rows available. WHERE then discards rows, GROUP BY forms groups, HAVING discards groups, SELECT computes the output columns, ORDER BY sorts them and LIMIT keeps the first few.
This order explains several rules that otherwise look arbitrary. WHERE cannot use an aggregate because grouping has not happened yet. WHERE usually cannot use a column alias defined in SELECT, because SELECT comes later, yet ORDER BY can, because it comes after. LIMIT applies last, so a query that sorts and then limits returns the true top ten, while a limit taken from an unsorted result returns any ten rows.
The sequence is logical rather than physical. The engine's optimiser is free to reorder work, use indexes or push filters earlier, provided the result is the same as if the steps had run in the logical order. Reading a query plan shows what actually happens; the logical order describes what the answer means.
Thinking in this order is a practical way to read an unfamiliar query, including one drafted by an AI assistant. Start at FROM and the joins and ask which rows exist, then apply each filter and grouping in turn, and only then look at the columns in SELECT. Most wrong answers can be traced to one of those steps doing something other than intended.
Worked example
An online bookshop in Leeds asks for the three genres with the highest 2026 revenue, counting only genres with at least 50 orders.
FROM orders: 10,000 rows.WHERE order_year = 2026: 4,000 rows remain.GROUP BY genre: 12 groups.HAVING COUNT(*) >= 50: 8 groups remain.SELECT genre, SUM(total) AS revenue;ORDER BY revenue DESC;LIMIT 3: three rows returned.
Walking the steps in logical order shows exactly which rows and groups each clause removed.
In practice
Analysts use the logical order to debug queries that give surprising totals, to explain to colleagues why an alias works in one clause but not another, and to check AI-drafted SQL step by step. Pandas code written as a chain of steps tends to follow the same sequence.
Watch out
- Written order and logical order differ; reading a query from the SELECT line downwards hides where rows were lost.
- Alias rules vary slightly between database products, so test before relying on an alias in WHERE or GROUP BY.
Learn it properly
Official sources
- The AQuA Book: guidance on producing quality analysis for government · HM Treasury
- Quality assurance of code for analysis and research · Government Analysis Function
Facts last checked on 9 November 2026.
Education, not advice: the Stockertim Dictionary explains what terms mean. It never recommends buying or selling anything.