← Back to SQL, page 17

sql-0340 · Easy fresher

A dashboard says 5 customers placed orders, but the shop has only 3 who did. Fix the count.

COUNT(customer_id) counts non-NULL values, one per order, not distinct customers. Use COUNT(DISTINCT customer_id) to count each customer once; it also skips the order with no customer.

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

The query as written:

sql
SELECT COUNT(customer_id) AS customers_with_orders FROM orders;
customers_with_orders
5

There are six orders. COUNT skips the one with a NULL customer and counts the other five, so customers with two orders (Ana and Bilal) are counted twice.

A corrected version:

sql
SELECT COUNT(DISTINCT customer_id) AS customers_with_orders FROM orders;
customers_with_orders
3

DISTINCT collapses repeat customers, giving 3: Ana, Bilal and Chloe. If cancelled orders should not count, add WHERE status <> 'cancelled' before counting. Labels on dashboards should say exactly what is counted.

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 COUNT(customer_id) AS customers_with_orders FROM orders;

SELECT COUNT(DISTINCT customer_id) AS customers_with_orders FROM orders;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • COUNT(col) counts non-NULL rows
  • COUNT(DISTINCT col) counts unique values
  • Decide whether cancelled orders count

Common mistakes

  • Using COUNT(*) for 'how many customers'
  • Counting distinct values after a fan-out join without noticing

Follow-ups

  • How would you count customers per month?
  • How expensive is COUNT(DISTINCT) on large tables?

Tips

  • Say what each COUNT counts