The Logical Order a SELECT Is Evaluated In, From FROM to LIMIT
A SELECT is written in one order and evaluated in another: FROM and JOIN first, then WHERE, then GROUP BY, then HAVING, then SELECT, then DISTINCT, then ORDER BY, and LIMIT last. This is the logical order, the meaning of the query, and it explains why you cannot use a SELECT alias in WHERE and why LIMIT cannot save you from a sort. The physical execution plan may reorder things, but only in ways that produce the same answer this order defines.
Questions this Concept answers
- Why can a column alias defined in `SELECT` not be referenced inside `WHERE`?
The Logical Processing Order of a SQL SELECT Statement from FROM Through LIMIT
A SQL `SELECT` statement is written in one order but evaluated in a different, fixed logical order — `FROM`/`JOIN`, `WHERE`, `GROUP BY`, `HAVING`, `SELECT`, `DISTINCT`, `ORDER BY`, `LIMIT`/`OFFSET` —…