An alias gives a column or table a temporary name for the duration of a query — for readability, or to rename computed columns and shorten long table references.
Syntax
SELECT column AS alias_name
FROM table_name AS alias_name;
-- AS is optional in most databases
SELECT column alias_name FROM table_name t;
Column Alias Example
SELECT name, salary * 12 AS annual_salary
FROM employees;
Output column header: annual_salary instead of the default, unreadable salary * 12.
Table Alias Example
SELECT e.name, d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.id;
Table aliases (e, d) make multi-table queries far more readable — essential once you're writing joins (covered in SQL Joins).
Practical Use Case
Aliases are used constantly: renaming computed values for clarity in reports, shortening long or repeated table names in joins, and giving subquery results a name (required in most databases — a subquery in FROM must be aliased).
Common Mistakes
- Using a column alias inside the same query's WHERE clause. Because
WHEREexecutes beforeSELECTin logical order, the alias doesn't exist yet at that point:
Most databases do allow referencing a SELECT alias in-- Usually invalid SELECT salary * 12 AS annual_salary FROM employees WHERE annual_salary > 700000; -- Fix: repeat the expression, or wrap in a subquery/CTE SELECT salary * 12 AS annual_salary FROM employees WHERE salary * 12 > 700000;ORDER BY, since that runs afterSELECT— the restriction is specifically aboutWHERE,GROUP BYandHAVINGin strict standard SQL (though MySQL is more lenient with GROUP BY/HAVING than some databases). - Forgetting quotes around an alias that contains spaces:
AS Total Salaryshould beAS "Total Salary"(or backticks in MySQL)
Interview Relevance
Understanding why an alias can't be used in WHERE ties directly back to SQL's logical execution order — a strong signal you understand the engine, not just the syntax.
Practice Question
Write a query that selects first_name and last_name concatenated as full_name, from a table aliased as e.