← Back to SQL, page 1

sql-0003 · Easy fresher

How do column aliases work in SQL, and how do you write an alias that contains a space?

An alias renames an output column or a table for the rest of the query, written with AS or simply after the expression. An alias with spaces or a reserved word must be quoted as an identifier, with double quotes in standard SQL. Single quotes make a string literal, not a name.

Full answer

An alias gives a name to an expression in the select list, or a short name to a table in FROM. AS is optional for both, but writing it for column aliases makes the code easier to read.

Here the computed column gets a readable heading, and the table gets the short alias e:

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
SELECT e.name AS "Employee name", e.salary / 12 AS "Monthly pay"
FROM employees AS e
WHERE e.dept_id = 1
ORDER BY "Employee name";
Employee nameMonthly pay
Asha10000
Mei7916
Ravi7916

If the alias has a space, quote it with double quotes, which is the standard way to quote identifiers. MySQL also accepts backticks, and SQL Server accepts square brackets, but double quotes work on PostgreSQL, SQLite and (with ANSI settings) the others.

A table alias also matters for correctness, not only for typing less. When the same table appears twice, as in a self join, the aliases are the only way to say which copy a column comes from:

sql
SELECT e.name AS employee, m.name AS manager
FROM employees e
JOIN employees m ON m.id = e.manager_id
WHERE e.dept_id = 1
ORDER BY e.name;
employeemanager
MeiAsha
RaviAsha

Two scoping rules are worth knowing. A column alias is visible in ORDER BY, but in standard SQL it is not visible in WHERE or GROUP BY of the same query. A table alias, once given, replaces the table name: after FROM employees e, PostgreSQL rejects employees.name in that query.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

SELECT e.name AS "Employee name", e.salary / 12 AS "Monthly pay"
FROM employees AS e
WHERE e.dept_id = 1
ORDER BY "Employee name";

SELECT e.name AS employee, m.name AS manager
FROM employees e
JOIN employees m ON m.id = e.manager_id
WHERE e.dept_id = 1
ORDER BY e.name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • AS is optional, but clearer for column aliases
  • Quote aliases with spaces using double quotes
  • Table aliases are required to tell apart two copies of the same table
  • Column aliases are visible in ORDER BY, not in standard WHERE

Common mistakes

  • Using single quotes for an alias, which some engines accept but others treat as a string
  • Referring to the original table name after giving it an alias

Follow-ups

  • How would you sort by an alias that has a space in it?
  • Why does a self join need table aliases?

Tips

  • Use short, meaningful table aliases (e for employees, d for departments), not a, b, c
  • Mention the scoping rule for WHERE versus ORDER BY