Sub topic 15
Why Clause Order Matters: How a Query Is Evaluated
Assume you know how tables, rows, and columns work, and that you can run basic SQL in PostgreSQL, MySQL, or SQLite. This chapter explains the logical order in which a database evaluates a query. That order helps you predict which names and expressions are available in each clause.
Written order and logical order
A query is usually written in this order:
SELECT first_name, salary * 12 AS annual_salary
FROM employees
WHERE department = 'Sales'
ORDER BY annual_salary DESC;The database logically processes the query in a different order:
FROMchooses the source table.WHEREremoves rows that do not match.SELECTchooses columns and calculates expressions.ORDER BYsorts the resulting rows.
The written order is designed to make queries readable. The logical order explains how the database builds the result.
Why aliases work in ORDER BY
The SELECT clause creates the alias annual_salary. Because ORDER BY is logically processed after SELECT, it can use that alias:
SELECT first_name, salary * 12 AS annual_salary
FROM employees
WHERE salary > 40000
ORDER BY annual_salary DESC;First, FROM reads employees. Next, WHERE keeps employees whose salary is greater than 40,000. Then, SELECT calculates annual salary and names the result. Finally, ORDER BY sorts the rows using that name.
Why aliases usually fail in WHERE
This query usually fails:
SELECT first_name, salary * 12 AS annual_salary
FROM employees
WHERE annual_salary > 600000;When WHERE runs, the SELECT list has not logically run yet. Therefore, annual_salary does not exist at that stage.
Repeat the expression in WHERE instead:
SELECT first_name, salary * 12 AS annual_salary
FROM employees
WHERE salary * 12 > 600000
ORDER BY annual_salary DESC;A database may support aliases in additional clauses, or may allow some special cases. Do not rely on those differences when writing portable SQL.
Filtering before sorting
WHERE filters rows before ORDER BY sorts them. This is useful because the database sorts only the rows that remain:
SELECT first_name, department, salary
FROM employees
WHERE department IN ('Sales', 'Marketing')
ORDER BY salary DESC;The result contains only employees from the two named departments, ordered from the highest salary to the lowest.
ORDER BY cannot bring back a row removed by WHERE. If a row does not satisfy the filter, it is gone from this query's result.
A practical debugging method
When a query gives an unexpected result, inspect it in logical stages:
- Run the
FROMandWHEREparts to check which rows remain. - Add the required columns to
SELECT. - Add calculated expressions and aliases.
- Add
ORDER BYlast. - Add a row limit only after the sort is correct.
For example, a top-five query needs ORDER BY before LIMIT. Otherwise, the database might choose five arbitrary rows before sorting them:
SELECT first_name, salary
FROM employees
ORDER BY salary DESC
LIMIT 5;Summary
FROM supplies rows, WHERE filters them, SELECT produces the requested values, and ORDER BY sorts the produced result. This order explains why a SELECT alias can usually be used in ORDER BY but not in WHERE.
Exercise
Why does this query fail in many SQL databases, and how can you correct it? SELECT salary * 12 AS annual_salary FROM employees WHERE annual_salary > 600000;
Show answer
It fails because WHERE is evaluated before SELECT creates annual_salary. Use WHERE salary * 12 > 600000, while keeping the alias in the SELECT list.