← Back to SQL, page 7

sql-0138 · Medium mid

A report uses SUM(CASE WHEN status = 'shipped' THEN 1 END) to count shipped orders per customer, and Dev shows NULL instead of 0. Why, and what is the fix?

A CASE without ELSE returns NULL for non-matching rows, and SUM of only NULLs is NULL. For Dev, the LEFT JOIN produced one row with NULL status, so the sum is NULL. Add ELSE 0, or use COUNT(CASE WHEN ... THEN 1 END), which returns 0 when nothing matches.

Full answer
sql
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);
sql
SELECT c.name, SUM(CASE WHEN o.status = 'shipped' THEN 1 END) AS shipped
FROM customers c LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name ORDER BY c.id;
nameshipped
Ana2
BilalNULL
Chloe1
DevNULL

Bilal's orders are cancelled and pending, so none matches; his SUM is NULL too. Dev has no orders, so his only row comes from the LEFT JOIN with all order columns NULL.

Two fixes, both giving 0:

sql
SELECT c.name,
       COUNT(CASE WHEN o.status = 'shipped' THEN 1 END)      AS shipped_count,
       SUM(CASE WHEN o.status = 'shipped' THEN 1 ELSE 0 END) AS shipped_sum
FROM customers c LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name ORDER BY c.id;
nameshipped_countshipped_sum
Ana22
Bilal00
Chloe11
Dev00

COUNT counts non-NULL values and returns 0 for an empty set, so it needs no ELSE. SUM with ELSE 0 also works. COALESCE(SUM(...), 0) is a third option, but fixing the expression itself is clearer.

sql
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 c.name, SUM(CASE WHEN o.status = 'shipped' THEN 1 END) AS shipped
FROM customers c LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name ORDER BY c.id;

SELECT c.name,
       COUNT(CASE WHEN o.status = 'shipped' THEN 1 END)      AS shipped_count,
       SUM(CASE WHEN o.status = 'shipped' THEN 1 ELSE 0 END) AS shipped_sum
FROM customers c LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name ORDER BY c.id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • CASE without ELSE yields NULL
  • SUM over only NULLs returns NULL, not 0
  • COUNT(CASE ...) returns 0 when nothing matches
  • ELSE 0 makes SUM return 0

Common mistakes

  • Assuming SUM of nothing is 0
  • Wrapping everything in COALESCE instead of fixing the expression

Follow-ups

  • What does AVG(CASE WHEN ... THEN 1 ELSE 0 END) compute?
  • How would FILTER express the same count?

Tips

  • Explain which rows are NULL and why