Interview Q&A / SQL & DBMS

BeginnerQuick answer1 min readUpdated 3 Oct 2026

Why can't you use a column alias in the WHERE clause?

SHORT ANSWER

WHERE runs before SELECT, so the alias does not exist yet when the filter is evaluated. Repeat the expression, or put the query in a subquery or CTE and filter outside.

Explain it simply

An alias is a nickname given while writing the final result. WHERE runs earlier, while rows are still being picked from the table, so the nickname has not been invented yet. It is like using a student's roll-call nickname before the teacher has assigned it.

Example

This fails in standard SQL:

sql
SELECT salary * 12 AS annual
FROM employees
WHERE annual > 500000;   -- error: unknown column "annual"

Fix 1, repeat the expression:

sql
SELECT salary * 12 AS annual
FROM employees
WHERE salary * 12 > 500000;

Fix 2, wrap it:

sql
SELECT *
FROM (SELECT salary * 12 AS annual FROM employees) t
WHERE annual > 500000;

ORDER BY can use the alias because it runs after SELECT. MySQL also lets GROUP BY and HAVING use aliases as an extension, but that is not portable.

What to say in the interview

"The logical order is FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY. WHERE comes before SELECT, so aliases are not visible there. I repeat the expression or use a derived table or CTE."

Common mistakes

  • Assuming MySQL's relaxed alias rules work in SQL Server or Oracle.

#sql#alias#where

Related questions

Practise more SQL & DBMS questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open SQL & DBMS quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.