Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to SQL Notes
Topic #212

SQL Aliases (AS)

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 WHERE executes before SELECT in logical order, the alias doesn't exist yet at that point:
    -- 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;
    Most databases do allow referencing a SELECT alias in ORDER BY, since that runs after SELECT — the restriction is specifically about WHERE, GROUP BY and HAVING in 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 Salary should be AS "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.

Related SQL Notes

Want to go beyond the notes?

Join CodingNow's SQL course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →