TExcelTAD
Home › SQL › SQL Aliases

SQL Aliases

An alias is a temporary name for a column or a table, valid only for the duration of the query. Aliases make results readable and long queries shorter.

sql
SELECT column AS alias_name FROM table_name;
SELECT columns FROM table_name AS alias_name;

Column aliases

Without aliases, calculated columns get ugly names — the expression itself:

sql · try it yourself
SELECT first_name || ' ' || last_name, salary / 12
FROM employees
WHERE employee_id <= 2;

SELECT first_name || ' ' || last_name AS full_name,
       salary / 12 AS monthly
FROM employees
WHERE employee_id <= 2;
first_name || ' ' || last_name | salary / 12
-------------------------------+------------
Grace Holt                     | 15416
Victor Ames                    | 8166
(2 rows)

full_name   | monthly
------------+--------
Grace Holt  | 15416
Victor Ames | 8166
(2 rows)

The keyword AS is optional (salary / 12 monthly), but writing it makes the intent clear.

Aliases with spaces

If an alias contains spaces or special characters, quote it with double quotes (standard SQL, SQLite, PostgreSQL), backticks (MySQL) or square brackets (SQL Server):

sql · try it yourself
SELECT name AS "Product name",
       price AS "Price (EUR)"
FROM products
WHERE category_id = 8
ORDER BY price;
Product name           | Price (EUR)
-----------------------+------------
The SQL Handbook       | 29
Python Recipes         | 34.5
Learn Rust in Practice | 39
(3 rows)

Table aliases

Table aliases shine when a query uses several tables. Instead of repeating customers. and orders., give each table a short name:

sql · try it yourself
SELECT o.order_id, c.name, c.city, o.status
FROM orders AS o
JOIN customers AS c ON c.customer_id = o.customer_id
WHERE c.country = 'Nigeria'
ORDER BY o.order_id;
order_id | name        | city  | status
---------+-------------+-------+----------
3        | Amara Costa | Lagos | shipped
6        | Amara Costa | Lagos | delivered
10       | Amara Costa | Lagos | shipped
20       | Amara Costa | Lagos | delivered
30       | Amara Costa | Lagos | shipped
(5 rows)

Once a table has an alias, use the alias everywhere in that query — in Oracle the AS keyword is not even allowed for tables (FROM orders o).

When the same table appears twice — for example employees and their managers — aliases are required to tell the copies apart:

sql · try it yourself
SELECT e.first_name AS employee, m.first_name AS manager
FROM employees AS e
JOIN employees AS m ON m.employee_id = e.manager_id
WHERE m.employee_id = 3
ORDER BY e.first_name;
employee | manager
---------+--------
Hana     | Lin
Marco    | Lin
(2 rows)

Where can you use a column alias?

The clauses run in a fixed order, which decides where an alias is visible:

Order Clause Can use SELECT aliases?
1 FROM / JOIN no
2 WHERE no (standard SQL)
3 GROUP BY no in standard SQL (yes in SQLite, MySQL, PostgreSQL)
4 HAVING no in standard SQL (yes in SQLite, MySQL)
5 SELECT defines them
6 ORDER BY yes
sql · try it yourself
SELECT name, ROUND(price * stock, 2) AS stock_value
FROM products
WHERE category_id = 6
ORDER BY stock_value DESC
LIMIT 3;
name                        | stock_value
----------------------------+------------
USB-C Cable                 | 1398.6
Noise-Cancelling Headphones | 1192
Mechanical Keyboard         | 1068
(3 rows)

Test yourself

Which alias syntax works for a column name with spaces in standard SQL?

  1. AS 'Total sales'
  2. AS "Total sales"
  3. AS Total sales
  4. AS (Total sales)

When is a table alias mandatory?

  1. Always
  2. When the same table appears twice in a query
  3. When using WHERE
  4. Never

In which clause can you always use a column alias from the SELECT list?

  1. WHERE
  2. FROM
  3. ORDER BY
  4. JOIN ... ON