SQL Aliases (AS)
An alias is a temporary name you give to a column or a table for the length of one query. Column aliases make result headers readable; table aliases make joins short and are required when a table appears twice. Neither changes anything in the database.
- Rename result columns with
AS - Give tables short aliases in joins
- Quote aliases that contain spaces
- Know which clauses can see an alias
Column aliases
SELECT first_name AS name, salary * 12 AS annual_salary, hire_date AS joinedFROM employeesWHERE dept_id = 3;| name | annual_salary | joined |
|---|---|---|
| Kavya | 1056000 | 2020-09-09 |
| Rohan | 540000 | 2024-01-22 |
first_name AS nameshows the column under the headername.salary * 12 AS annual_salarynames a calculated column; without the alias its header would besalary * 12.- The table’s real column names do not change.
AS is optional: salary * 12 annual_salary works the same. Writing AS makes the intent obvious and avoids the missing-comma trap.
Aliases with spaces
Wrap an alias in quotes or backticks when it contains spaces:
SELECT customer_name AS 'Customer Name', city AS "Home City"FROM customersLIMIT 3;| Customer Name | Home City |
|---|---|
| Ananya Sharma | Mumbai |
| Rohit Kumar | Delhi |
| Fatima Khan | Hyderabad |
Use spaces in aliases only for final reports. In application code use snake_case aliases such as annual_salary, so they are easy to read in code.
Table aliases
A table alias is a short name for a table, written after it in FROM or JOIN. Columns are then written as alias.column:
SELECT e.first_name, d.dept_name, d.locationFROM employees AS eJOIN departments AS d ON d.dept_id = e.dept_idWHERE d.location = 'Pune';| first_name | dept_name | location |
|---|---|---|
| Kavya | Marketing | Pune |
| Rohan | Marketing | Pune |
employees AS eanddepartments AS dgive the tables one-letter names.e.first_namemeans “thefirst_namecolumn ofemployees“.- The join pairs each employee with their department;
WHEREkeeps the department located in Pune (Marketing).
With aliases, a column that exists in both tables, such as dept_id, is never ambiguous. Without a prefix MySQL refuses the query:
SELECT dept_id, first_name, dept_nameFROM employeesJOIN departments ON departments.dept_id = employees.dept_id;ERROR 1052 (23000): Column 'dept_id' in field list is ambiguous
When an alias is required: self join
When the same table appears twice, aliases are the only way to tell the copies apart. Each employee with their manager’s name:
SELECT e.first_name AS employee, m.first_name AS managerFROM employees eJOIN employees m ON m.emp_id = e.manager_idORDER BY e.emp_id;| employee | manager |
|---|---|
| Asha | Meera |
| Rahul | Asha |
| Priya | Asha |
| Karan | Rahul |
| Vikram | Meera |
| Neha | Vikram |
| Arjun | Vikram |
| Kavya | Meera |
| Rohan | Kavya |
| Sneha | Meera |
| Amit | Asha |
employees eis the employee copy;employees mis the manager copy.m.emp_id = e.manager_idpairs each employee with the row of their manager.- The CEO has no manager, so she does not appear (that is how an inner join works); see SELF JOIN.
Where can an alias be used?
| Clause | Column alias | Table alias |
|---|---|---|
SELECT | defined here | yes |
FROM / JOIN ... ON | no | defined here |
WHERE | no | yes |
GROUP BY / HAVING | yes in MySQL (not standard) | yes |
ORDER BY | yes | yes |
SELECT category, COUNT(*) AS productsFROM productsGROUP BY categoryHAVING products >= 3ORDER BY products DESC;| category | products |
|---|---|
| Furniture | 4 |
| Electronics | 3 |
COUNT(*) AS productscounts the products of each category.- MySQL lets
HAVINGandORDER BYuse the aliasproducts. Standard SQL would requireHAVING COUNT(*) >= 3. - Stationery has only 2 products, so it is filtered out; Furniture (4) and Electronics (3) remain.
Exercise: Readable report
Show each product’s product_name as Product, price as Price (INR) and the stock value price * stock as Stock Value.
| Product | Price (INR) | Stock Value |
|---|---|---|
| Wireless Mouse | 799 | 95880 |
| Mechanical Keyboard | 3499 | 157455 |
| USB-C Hub | 1999 | 0 |
| Office Chair | 8999 | 107988 |
| Standing Desk | 18999 | 94995 |
| Notebook Pack | 249 | 74700 |
| Gel Pens | 199 | 49750 |
| Desk Lamp | 1499 | 44970 |
| Monitor Arm | 2799 | 50382 |
Hint 1
Aliases with spaces or brackets need quotes.
SELECT product_name AS Product, price AS 'Price (INR)', price * stock AS 'Stock Value'FROM products;| Product | Price (INR) | Stock Value |
|---|---|---|
| Wireless Mouse | 799 | 95880 |
| Mechanical Keyboard | 3499 | 157455 |
| USB-C Hub | 1999 | 0 |
| Office Chair | 8999 | 107988 |
| Standing Desk | 18999 | 94995 |
| Notebook Pack | 249 | 74700 |
| Gel Pens | 199 | 49750 |
| Desk Lamp | 1499 | 44970 |
| Monitor Arm | 2799 | 50382 |
Quick quiz
1. Does an alias change the column name in the table?
Aliases exist only while the query runs.
2. Why does WHERE annual_salary > 1000000 fail when annual_salary is a SELECT alias?
The alias does not exist yet when WHERE is evaluated.
3. When is a table alias required?
A self join needs aliases to tell the two copies apart.
Alias FAQ
What is an alias in SQL?
An alias is a temporary name for a column or table, created with AS, that is valid only for one query.
Is the AS keyword required for aliases?
No. SELECT salary * 12 annual works, but writing AS is clearer. Oracle does not allow AS before table aliases.
Can I use a column alias in the WHERE clause?
No. WHERE is evaluated before SELECT. Repeat the expression or wrap the query in a subquery or CTE.
Why use table aliases?
They shorten queries with joins, make it clear which table each column comes from, and are required in self joins.