Sub topic 7
The Logical Order of Query Evaluation
This chapter assumes you know basic SELECT, FROM, WHERE, and ORDER BY syntax. You should also understand tables, rows, columns, NULL, and basic joins.
SQL statements are written in one order, but the database logically processes them in another order. Understanding that order explains why some aliases work in ORDER BY but fail in WHERE, and why WHERE cannot filter an aggregate such as SUM().
The logical processing order
A grouped query is logically evaluated in this order:
FROMandJOINchoose the source rows.WHEREremoves individual rows.GROUP BYforms groups from the remaining rows.HAVINGremoves complete groups.SELECTcalculates the output columns.DISTINCTremoves duplicate output rows.ORDER BYsorts the result.LIMITorFETCHkeeps only part of the sorted result.
This is a logical model. Database engines may physically rearrange operations internally when the result stays the same.
Why `WHERE` and `HAVING` are different
WHERE runs before grouping. It can inspect each source row, but it cannot use the result of an aggregate calculation for a group.
HAVING runs after grouping. It can inspect aggregate results such as SUM(amount) or COUNT(*).
SELECT customer_id,
SUM(amount) AS total_amount
FROM orders
WHERE status = 'paid'
GROUP BY customer_id
HAVING SUM(amount) > 1000
ORDER BY total_amount DESC;The query first joins or reads orders. It then keeps only paid rows. Next, it creates one group for each customer_id. It removes groups whose paid total is not greater than 1000. Finally, it names and sorts the selected output.
This common mistake does not work:
SELECT customer_id,
SUM(amount) AS total_amount
FROM orders
WHERE SUM(amount) > 1000
GROUP BY customer_id;At the point when WHERE runs, groups do not exist yet. The database has only individual rows.
Aliases and processing order
The alias total_amount is created during the logical SELECT step. Therefore, standard SQL cannot use that alias in WHERE, because WHERE runs earlier.
Many SQL systems do allow the alias in ORDER BY, because ORDER BY runs later:
SELECT customer_id,
SUM(amount) AS total_amount
FROM orders
GROUP BY customer_id
ORDER BY total_amount DESC;For portable SQL, repeat the aggregate in HAVING:
SELECT customer_id,
SUM(amount) AS total_amount
FROM orders
GROUP BY customer_id
HAVING SUM(amount) > 1000;Some database systems permit a SELECT alias in HAVING, but that behavior differs across systems. Do not rely on it when writing portable queries.
Why this model helps
The logical order gives you a debugging method:
- Check
FROMandJOINif rows are duplicated or missing. - Check
WHEREif input rows were removed too early. - Check
GROUP BYif groups are too broad or too narrow. - Check
HAVINGif complete groups are missing. - Check
SELECTif calculated values or aliases are wrong. - Check
ORDER BYandLIMITonly after confirming the result set is correct.
Exercise
Which clause should filter customers whose total paid order amount exceeds 1000: WHERE or HAVING?
Show answer
HAVING, because the condition uses an aggregate calculated after GROUP BY.