← Back to SQL, page 4

sql-0080 · Medium mid

HAVING COUNT(*) > 1 works, but can HAVING refer to a SELECT alias, as in HAVING n > 1?

Standard SQL evaluates HAVING before SELECT, so a SELECT alias is not visible there and PostgreSQL rejects HAVING n > 1. SQLite and MySQL accept the alias as an extension. Repeating the aggregate in HAVING works everywhere.

Full answer
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 resolves the alias:

sql
SELECT dept_id, COUNT(*) AS n FROM employees GROUP BY dept_id HAVING n > 1 ORDER BY dept_id;
dept_idn
13
22
32

PostgreSQL does not:

sql
SELECT dept_id, COUNT(*) AS n FROM employees GROUP BY dept_id HAVING n > 1;  -- error on purpose
text
Error: column "n" does not exist

The reason is the logical order: FROM, WHERE, GROUP BY, HAVING, then SELECT. When HAVING runs, the alias n has not been defined yet. PostgreSQL follows that order strictly for HAVING (it does allow aliases in GROUP BY and ORDER BY as extensions).

The portable form repeats the aggregate:

sql
SELECT dept_id, COUNT(*) AS n FROM employees GROUP BY dept_id HAVING COUNT(*) > 1 ORDER BY dept_id;
dept_idn
13
22
32

Repeating COUNT(*) does not make the database count twice; the planner recognises the same expression. When the expression is long, wrap the grouped query in a subquery or CTE and filter the alias in an outer WHERE.

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 dept_id, COUNT(*) AS n FROM employees GROUP BY dept_id HAVING n > 1 ORDER BY dept_id;

SELECT dept_id, COUNT(*) AS n FROM employees GROUP BY dept_id HAVING COUNT(*) > 1 ORDER BY dept_id;

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

Key points

  • HAVING runs before SELECT in the logical order
  • PostgreSQL rejects SELECT aliases in HAVING
  • SQLite and MySQL accept them as an extension
  • Repeat the aggregate, or filter in an outer query

Common mistakes

  • Testing HAVING alias on SQLite and failing on PostgreSQL
  • Assuming the repeated aggregate is computed twice

Follow-ups

  • Can GROUP BY use a SELECT alias in PostgreSQL?
  • How would you write this with a CTE?

Tips

  • Explain with the logical order rather than memorising engine rules