SQL Tutorial › Querying Data › Aliases

SQL Aliases (AS)

BeginnerLesson 16 of 698 min read6 queries run on MySQL 8.4

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.

Advertisement
In this lesson
  • 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

SQL
SELECT first_name AS name,       salary * 12 AS annual_salary,       hire_date   AS joinedFROM employeesWHERE dept_id = 3;
ResultMySQL 8.4
nameannual_salaryjoined
Kavya10560002020-09-09
Rohan5400002024-01-22
2 rows in set
How this query works, step by step
  1. first_name AS name shows the column under the header name.
  2. salary * 12 AS annual_salary names a calculated column; without the alias its header would be salary * 12.
  3. 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:

SQL
SELECT customer_name AS 'Customer Name',       city          AS "Home City"FROM customersLIMIT 3;
ResultMySQL 8.4
Customer NameHome City
Ananya SharmaMumbai
Rohit KumarDelhi
Fatima KhanHyderabad
3 rows in set
Tip

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:

SQL
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';
ResultMySQL 8.4
first_namedept_namelocation
KavyaMarketingPune
RohanMarketingPune
2 rows in set
How this query works, step by step
  1. employees AS e and departments AS d give the tables one-letter names.
  2. e.first_name means “the first_name column of employees“.
  3. The join pairs each employee with their department; WHERE keeps 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:

MySQL
SELECT dept_id, first_name, dept_nameFROM employeesJOIN departments ON departments.dept_id = employees.dept_id;
ResultMySQL 8.4
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:

SQL
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;
ResultMySQL 8.4
employeemanager
AshaMeera
RahulAsha
PriyaAsha
KaranRahul
VikramMeera
NehaVikram
ArjunVikram
KavyaMeera
RohanKavya
SnehaMeera
AmitAsha
11 rows in set
How this query works, step by step
  1. employees e is the employee copy; employees m is the manager copy.
  2. m.emp_id = e.manager_id pairs each employee with the row of their manager.
  3. 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?

ClauseColumn aliasTable alias
SELECTdefined hereyes
FROM / JOIN ... ONnodefined here
WHEREnoyes
GROUP BY / HAVINGyes in MySQL (not standard)yes
ORDER BYyesyes
SQL
SELECT category, COUNT(*) AS productsFROM productsGROUP BY categoryHAVING products >= 3ORDER BY products DESC;
ResultMySQL 8.4
categoryproducts
Furniture4
Electronics3
2 rows in set
How this query works, step by step
  1. COUNT(*) AS products counts the products of each category.
  2. MySQL lets HAVING and ORDER BY use the alias products. Standard SQL would require HAVING COUNT(*) >= 3.
  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.

Expected resultMySQL 8.4
ProductPrice (INR)Stock Value
Wireless Mouse79995880
Mechanical Keyboard3499157455
USB-C Hub19990
Office Chair8999107988
Standing Desk1899994995
Notebook Pack24974700
Gel Pens19949750
Desk Lamp149944970
Monitor Arm279950382
9 rows in set
Hint 1

Aliases with spaces or brackets need quotes.

Quick quiz

1. Does an alias change the column name in the table?

2. Why does WHERE annual_salary > 1000000 fail when annual_salary is a SELECT alias?

3. When is a table alias required?

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.