← Back to SQL, page 14

sql-0265 · Medium mid

Write a query that shows, for each day, the number of orders and the number of distinct customers, including days with no orders, for 2026-03-01 to 2026-03-12.

Generate the date range with a recursive CTE, LEFT JOIN orders on the date, and aggregate with COUNT(o.id) and COUNT(DISTINCT o.customer_id). Counting a column from the orders side gives 0 on empty days, while COUNT(*) would count the calendar row and return 1.

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
WITH RECURSIVE d(day) AS (
  SELECT '2026-03-01' UNION ALL SELECT date(day, '+1 day') FROM d WHERE day < '2026-03-12'
)
SELECT d.day, COUNT(o.id) AS orders, COUNT(DISTINCT o.customer_id) AS customers
FROM d LEFT JOIN orders o ON o.order_date = d.day
GROUP BY d.day
HAVING d.day IN ('2026-03-01', '2026-03-02', '2026-03-09', '2026-03-12')
ORDER BY d.day;
dayorderscustomers
2026-03-0111
2026-03-0200
2026-03-0911
2026-03-1210

Only three days in the range have orders. COUNT(o.id) returns 0 elsewhere because the joined order columns are NULL. COUNT(*) would return 1 for those days, which is a classic mistake in gap-filled reports.

The guest order on 12 March has no customer, so it counts as an order but adds nothing to distinct customers.

In PostgreSQL, generate_series(DATE '2026-03-01', DATE '2026-03-12', INTERVAL '1 day') replaces the recursive CTE.

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

WITH RECURSIVE d(day) AS (
  SELECT '2026-03-01' UNION ALL SELECT date(day, '+1 day') FROM d WHERE day < '2026-03-12'
)
SELECT d.day, COUNT(o.id) AS orders, COUNT(DISTINCT o.customer_id) AS customers
FROM d LEFT JOIN orders o ON o.order_date = d.day
GROUP BY d.day
HAVING d.day IN ('2026-03-01', '2026-03-02', '2026-03-09', '2026-03-12')
ORDER BY d.day;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Generate the calendar, then LEFT JOIN
  • COUNT a column from the joined side
  • COUNT(*) counts the calendar row on empty days
  • COUNT(DISTINCT) ignores NULL customers

Common mistakes

  • Using COUNT(*) after a LEFT JOIN
  • Filtering the joined table in WHERE and losing empty days

Follow-ups

  • How would you show a 7-day rolling count?
  • How would you do this per country?

Tips

  • Point out COUNT(*) versus COUNT(col)