Full answer
An aggregate without GROUP BY always returns exactly one row, even when no input rows match. What goes in that row differs by function:
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);SELECT COUNT(*) AS n, SUM(qty) AS total_qty, MAX(qty) AS biggest
FROM order_items
WHERE product = 'Laptop';| n | total_qty | biggest |
|---|---|---|
| 0 | NULL | NULL |
COUNT says zero rows matched. SUM says "no values to add", which SQL represents as NULL. That surprises people who expect the sum of nothing to be zero, and it shows up as a blank on dashboards or as a NULL that breaks later arithmetic.
When zero is the correct business answer, say so:
SELECT COALESCE(SUM(qty), 0) AS total_qty, TOTAL(qty) AS sqlite_total
FROM order_items
WHERE product = 'Laptop';| total_qty | sqlite_total |
|---|---|
| 0 | 0.0 |
SQLite has an extra aggregate, TOTAL(), which behaves like SUM but returns 0.0 for an empty set and always returns a floating-point number.
With GROUP BY, an empty input gives no rows at all, not a row of zeros. To show every customer with a 0 count, start from the customers table and LEFT JOIN orders:
SELECT c.name, COUNT(o.id) AS orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name
ORDER BY c.name;| name | orders |
|---|---|
| Ana | 2 |
| Bilal | 2 |
| Chloe | 1 |
| Dev | 0 |
Dev appears with 0 only because the query starts from customers. Grouping the orders table alone could never produce a row for him.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
SELECT COUNT(*) AS n, SUM(qty) AS total_qty, MAX(qty) AS biggest
FROM order_items
WHERE product = 'Laptop';
SELECT COALESCE(SUM(qty), 0) AS total_qty, TOTAL(qty) AS sqlite_total
FROM order_items
WHERE product = 'Laptop';
SELECT c.name, COUNT(o.id) AS orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name
ORDER BY c.name;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- COUNT of no rows is 0
- SUM, AVG, MIN, MAX of no rows are NULL
- COALESCE(SUM(x), 0) when zero is meaningful
- GROUP BY over no rows returns no rows; LEFT JOIN from the parent table to show zeros
Common mistakes
- Adding NULL from an empty SUM to another number and getting NULL
- Expecting GROUP BY to produce rows for groups that have no data
Follow-ups
- What does AVG return over an empty set?
- How would you list every month of the year with its revenue, including months with none?
Tips
- Show the LEFT JOIN version; the empty-group case is the follow-up interviewers ask