← Back to SQL, page 6

sql-0101 · Easy fresher

Does PostgreSQL or SQLite require an alias for a subquery in the FROM clause?

SQLite never required one, and PostgreSQL has not required one since version 16, so SELECT * FROM (SELECT ...) works on both. MySQL and older PostgreSQL versions require an alias. Giving every derived table a short, meaningful alias is good practice anyway, because you need it to qualify columns in joins.

Full answer

A subquery in FROM is called a derived table: its result is used like a table for the rest of the 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');

SQLite accepts it without an alias:

sql
SELECT MAX(n) AS biggest_team
FROM (SELECT dept_id, COUNT(*) AS n FROM employees GROUP BY dept_id);
biggest_team
3

So does PostgreSQL 17:

sql
SELECT MAX(n) AS biggest_team
FROM (SELECT dept_id, COUNT(*) AS n FROM employees GROUP BY dept_id);
biggest_team
3

PostgreSQL 15 and earlier raised "subquery in FROM must have an alias", and MySQL still requires one ("Every derived table must have its own alias"); those come from their documentation and were not run here.

An alias costs nothing and pays off as soon as the derived table is joined, because columns then need a prefix:

sql
SELECT d.name, t.n
FROM departments d
JOIN (SELECT dept_id, COUNT(*) AS n FROM employees GROUP BY dept_id) AS t
  ON t.dept_id = d.id
ORDER BY t.n DESC, d.name;
namen
Engineering3
Sales2
Support2

Portable code should always alias derived tables.

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 MAX(n) AS biggest_team
FROM (SELECT dept_id, COUNT(*) AS n FROM employees GROUP BY dept_id);

SELECT d.name, t.n
FROM departments d
JOIN (SELECT dept_id, COUNT(*) AS n FROM employees GROUP BY dept_id) AS t
  ON t.dept_id = d.id
ORDER BY t.n DESC, d.name;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • A subquery in FROM is a derived table
  • SQLite and PostgreSQL 16+ allow it without an alias
  • MySQL and PostgreSQL 15 and earlier require one
  • Always alias derived tables in portable code

Common mistakes

  • Writing alias-free derived tables and breaking on MySQL or older PostgreSQL
  • Using meaningless aliases like t1, t2 in long queries

Follow-ups

  • When would you use a CTE instead of a derived table?
  • Can a derived table refer to columns of other tables in the same FROM clause?

Tips

  • Quote the version boundary for PostgreSQL; it shows you keep up with releases