Learn SQL Lesson

Why SELECT Aliases Work in ORDER BY

`SELECT` runs later than `WHERE` but earlier than `ORDER BY`.

That has an important consequence:

• In standard SQL, `WHERE` cannot use a `SELECT` alias • `ORDER BY` can use a `SELECT` alias

Example:

SELECT name, salary * 0.10 AS annual_bonus
FROM employees
ORDER BY annual_bonus DESC

By the time `ORDER BY` runs, `annual_bonus` already exists in the result shape, so sorting by it is allowed.

DuckDB is permissive with some aliases in earlier clauses, but this standard logical-order rule is the most portable habit to learn.

Practice challenge

Return name and annual_bonus, where annual_bonus is salary * 0.10. Order the result by annual_bonus descending.

Open interactive editor