← Back to SQL, page 19

sql-0372 · Easy fresher

A monthly order report shows one row for most of the year instead of one per month. Find the bug.

substr(order_date, 1, 6) keeps only '2026-0', which is the same for January to September, so those months merge into one group. Use substr(order_date, 1, 7) or strftime('%Y-%m', order_date) to keep the full year and month.

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 substr(order_date, 1, 6) AS month, COUNT(*) AS orders FROM orders GROUP BY month ORDER BY month;
monthorders
2026-06

All six orders fall in January to March, and every one of those dates starts with '2026-0'. The report shows a single row with 6 orders, hiding the monthly pattern completely.

A corrected version:

sql
SELECT strftime('%Y-%m', order_date) AS month, COUNT(*) AS orders FROM orders GROUP BY month ORDER BY month;
monthorders
2026-011
2026-022
2026-033

With the full 'YYYY-MM' key there is one row per month: one order in January, two in February and three in March. strftime documents the intent better than counting characters, and in PostgreSQL the equivalent is date_trunc('month', order_date) or to_char(order_date, 'YYYY-MM').

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 substr(order_date, 1, 6) AS month, COUNT(*) AS orders FROM orders GROUP BY month ORDER BY month;

SELECT strftime('%Y-%m', order_date) AS month, COUNT(*) AS orders FROM orders GROUP BY month ORDER BY month;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Check the length of substring keys
  • strftime('%Y-%m', ...) groups by month in SQLite
  • date_trunc or to_char in PostgreSQL

Common mistakes

  • Testing on data from a single month
  • Grouping by a key that does not include the year

Follow-ups

  • How would you show months with zero orders?
  • How would you group by ISO week?

Tips

  • Show the merged key