$ ./guess_the_languageSQL & Databases
SQL & Databases · medium · #5

Why does PostgreSQL reject this query?

-- PostgreSQL
SELECT price * qty AS total
FROM items
WHERE total > 100;
-- ERROR: column "total" does not exist
  1. total is a reserved keyword
  2. WHERE runs before the alias exists
  3. AS aliases need double quotes
  4. Expressions can't be aliased in SELECT
Logically WHERE is evaluated before the SELECT list, so its aliases are not visible; repeat the expression or use a subquery.
Play SQL & Databases ▸