SELECT price * qty AS total
FROM order_items
WHERE total > 100;
WHERE is evaluated before the SELECT list, so the alias does not exist yetWHERE runs before the SELECT list, so the alias total does not exist yet.
SQL runs a query in this logical order: FROM, WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, LIMIT. Aliases are created in the SELECT step. So WHERE cannot see them, but ORDER BY can.
It is not that "Computed expressions cannot be compared in a WHERE clause": you can compare the expression itself. It is also not about "Column aliases are only allowed when the query has a GROUP BY".
Fixes:
• Repeat the expression: WHERE price * qty > 100.
• Or wrap the query in a subquery or a CTE (a named subquery written with WITH).
Read more