← Back to SQL, page 4

sql-0075 · Medium mid

Write a query showing, for each customer, how many orders are shipped, pending and cancelled, in a single row per customer.

Use conditional aggregation: SUM(CASE WHEN status = 'shipped' THEN 1 ELSE 0 END) and the same for each status, grouped by customer. The standard FILTER clause, COUNT(*) FILTER (WHERE status = 'shipped'), says the same thing more directly and works in PostgreSQL and SQLite 3.30+.

Full answer

Conditional aggregation turns rows into columns, a small pivot. The CASE inside the aggregate decides, row by row, whether the row counts towards that column.

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 ELSE 0 END) AS shipped,
       SUM(CASE WHEN o.status = 'pending'   THEN 1 ELSE 0 END) AS pending,
       SUM(CASE WHEN o.status = 'cancelled' THEN 1 ELSE 0 END) AS cancelled
FROM customers c
JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name
ORDER BY c.name;
nameshippedpendingcancelled
Ana200
Bilal011
Chloe100

How it works for Bilal: his two orders produce two rows. For the shipped column, CASE gives 0 and 0, so the sum is 0. For pending it gives 1 and 0, and for cancelled 0 and 1.

The FILTER clause expresses the same idea without CASE:

sql
SELECT c.name,
       COUNT(*) FILTER (WHERE o.status = 'shipped')   AS shipped,
       COUNT(*) FILTER (WHERE o.status = 'pending')   AS pending,
       COUNT(*) FILTER (WHERE o.status = 'cancelled') AS cancelled
FROM customers c
JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name
ORDER BY c.name;
nameshippedpendingcancelled
Ana200
Bilal011
Chloe100

PostgreSQL gives the same result for the FILTER version:

sql
SELECT c.name,
       COUNT(*) FILTER (WHERE o.status = 'shipped')   AS shipped,
       COUNT(*) FILTER (WHERE o.status = 'pending')   AS pending,
       COUNT(*) FILTER (WHERE o.status = 'cancelled') AS cancelled
FROM customers c
JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name
ORDER BY c.name;
nameshippedpendingcancelled
Ana200
Bilal011
Chloe100

Both forms scan the table once, which is the main reason to use them over three separate queries or three joins. In MySQL, which has no FILTER clause, SUM(status = 'shipped') also works because comparisons return 1 or 0 there (from MySQL documentation, not run here); SQLite accepts that trick too, but it is less readable than CASE.

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

SELECT c.name,
       COUNT(*) FILTER (WHERE o.status = 'shipped')   AS shipped,
       COUNT(*) FILTER (WHERE o.status = 'pending')   AS pending,
       COUNT(*) FILTER (WHERE o.status = 'cancelled') AS cancelled
FROM customers c
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. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • CASE inside SUM or COUNT counts only matching rows
  • One pass over the table produces several metrics
  • COUNT(*) FILTER (WHERE ...) is the standard alternative
  • This is the usual way to pivot a known set of values

Common mistakes

  • Writing COUNT(CASE WHEN ... THEN 1 ELSE 0 END), which counts every row because 0 is not NULL
  • Running one query per status and stitching results in application code

Follow-ups

  • How would you add revenue per status as well?
  • How would you pivot when the list of statuses is not known in advance?

Tips

  • Point out the COUNT versus SUM trap with ELSE 0