← Back to SQL, page 14

sql-0267 · Easy fresher

How would you return customers who ordered in January but not in February?

Find January customers and remove February customers: EXCEPT between two SELECT DISTINCT customer_id queries, or NOT EXISTS for a February order per January customer. Use date ranges so the month filters stay index-friendly, and exclude NULL guest customers.

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 customer_id FROM orders WHERE order_date >= '2026-01-01' AND order_date < '2026-02-01'
EXCEPT
SELECT customer_id FROM orders WHERE order_date >= '2026-02-01' AND order_date < '2026-03-01';
customer_id

(0 rows)

Ana ordered on 5 January and again on 11 February, so she does not qualify. No other customer ordered in January, so the result is empty, which is a valid answer. Changing the second month to March shows the shape of a non-empty result:

sql
SELECT customer_id FROM orders WHERE order_date >= '2026-01-01' AND order_date < '2026-02-01'
EXCEPT
SELECT customer_id FROM orders WHERE order_date >= '2026-03-01' AND order_date < '2026-04-01';
customer_id
1

The NOT EXISTS form returns the same rows and is easier to extend with extra columns:

text
SELECT DISTINCT o.customer_id FROM orders o
WHERE o.order_date >= '2026-01-01' AND o.order_date < '2026-02-01'
  AND NOT EXISTS (SELECT 1 FROM orders x WHERE x.customer_id = o.customer_id
                  AND x.order_date >= '2026-02-01' AND x.order_date < '2026-03-01');
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 customer_id FROM orders WHERE order_date >= '2026-01-01' AND order_date < '2026-02-01'
EXCEPT
SELECT customer_id FROM orders WHERE order_date >= '2026-02-01' AND order_date < '2026-03-01';

SELECT customer_id FROM orders WHERE order_date >= '2026-01-01' AND order_date < '2026-02-01'
EXCEPT
SELECT customer_id FROM orders WHERE order_date >= '2026-03-01' AND order_date < '2026-04-01';

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • EXCEPT removes one set from another
  • NOT EXISTS is the per-row equivalent
  • Use half-open date ranges
  • Empty results can be correct

Common mistakes

  • Using NOT IN against a column with NULL guests
  • Filtering months with substr and losing index use

Follow-ups

  • How would you find customers active in every month?
  • How would you count churned customers per month?

Tips

  • Check the empty-result case