← Back to SQL, page 5

sql-0086 · Medium mid

Can you use HAVING without GROUP BY? What does SELECT COUNT(*) FROM employees HAVING COUNT(*) > 100 return?

Yes. Without GROUP BY, the whole table is one group, and HAVING decides whether that single group is returned. With 8 employees, HAVING COUNT(*) > 100 is false, so the query returns zero rows instead of a row with the count. It is a compact way to return a result only when a condition about the whole set holds.

Full answer

An aggregate query without GROUP BY normally returns exactly one row. HAVING can remove that row.

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');

CREATE TABLE order_items (order_id INTEGER, qty INTEGER);
CREATE TABLE orders (id INTEGER PRIMARY KEY);
INSERT INTO orders VALUES (1), (2);
INSERT INTO order_items VALUES (1, 3), (2, 1);
sql
SELECT COUNT(*) AS n FROM employees HAVING COUNT(*) > 100;
n

(0 rows)

sql
SELECT COUNT(*) AS n FROM employees HAVING COUNT(*) > 5;
n
8

The first query returns nothing: the single group has 8 rows, which fails the condition. The second returns the row because the condition holds.

A practical use is a data check that should return rows only when something is wrong, for example "return the count of orders with no items, but only if there are any":

sql
SELECT COUNT(*) AS orders_without_items
FROM orders o
WHERE NOT EXISTS (SELECT 1 FROM order_items i WHERE i.order_id = o.id)
HAVING COUNT(*) > 0;
orders_without_items

(0 rows)

Zero rows means the check passed; a monitoring job can alert whenever the query returns a row.

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');

CREATE TABLE order_items (order_id INTEGER, qty INTEGER);
CREATE TABLE orders (id INTEGER PRIMARY KEY);
INSERT INTO orders VALUES (1), (2);
INSERT INTO order_items VALUES (1, 3), (2, 1);

SELECT COUNT(*) AS n FROM employees HAVING COUNT(*) > 100;

SELECT COUNT(*) AS n FROM employees HAVING COUNT(*) > 5;

SELECT COUNT(*) AS orders_without_items
FROM orders o
WHERE NOT EXISTS (SELECT 1 FROM order_items i WHERE i.order_id = o.id)
HAVING COUNT(*) > 0;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Without GROUP BY, the whole input is one group
  • HAVING can remove that single row
  • The result is zero rows, not a row containing 0
  • Useful for checks that return rows only on failure

Common mistakes

  • Expecting a row with a count when HAVING fails
  • Confusing 'zero rows' with 'a row with value 0' in application code

Follow-ups

  • What does SELECT COUNT(*) FROM empty_table return?
  • How would you write the check with WHERE EXISTS instead?

Tips

  • Contrast zero rows with a row containing 0