Full answer
The two clauses run at different moments. WHERE sees raw rows. HAVING sees groups, after aggregates have been computed.
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');"Departments with more than one employee earning over 65000":
SELECT dept_id, COUNT(*) AS well_paid
FROM employees
WHERE salary > 65000
GROUP BY dept_id
HAVING COUNT(*) > 1
ORDER BY dept_id;| dept_id | well_paid |
|---|---|
| 1 | 3 |
salary > 65000 is about each employee, so it goes in WHERE and runs first. COUNT(*) > 1 is about each department's group, so it goes in HAVING. Sales is left with only Tom after WHERE, so it fails HAVING.
Putting the aggregate in WHERE is an error, because no groups exist yet when WHERE runs:
SELECT dept_id FROM employees WHERE COUNT(*) > 1 GROUP BY dept_id; -- error on purposeError: misuse of aggregate: COUNT()The reverse, moving a row condition into HAVING, often works but is wasteful and sometimes not allowed. Standard SQL lets HAVING use only grouped columns and aggregates, so HAVING dept_id = 1 is fine, but HAVING salary > 60000 is not, because salary is not one value per group:
SELECT dept_id, COUNT(*) FROM employees GROUP BY dept_id HAVING salary > 60000; -- error on purposeError: column "employees.salary" must appear in the GROUP BY clause or be used in an aggregate functionCREATE 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 well_paid
FROM employees
WHERE salary > 65000
GROUP BY dept_id
HAVING COUNT(*) > 1
ORDER BY dept_id;
-- error on purpose
SELECT dept_id FROM employees WHERE COUNT(*) > 1 GROUP 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
- WHERE filters rows before grouping
- HAVING filters groups after aggregates are computed
- Aggregates are not allowed in WHERE
- Put row conditions in WHERE so less data is grouped
Common mistakes
- Writing WHERE COUNT(*) > 1
- Moving every condition into HAVING out of habit
Follow-ups
- Can HAVING be used without GROUP BY?
- Can HAVING refer to a column alias from SELECT?
Tips
- Name the evaluation order: WHERE, then GROUP BY, then HAVING