Skip to content
Quantoros

Return customerids with at least one order in January 2026 but NONE in February 2026. Table definitions (from the…

SQLLevel: IntermediateMembership and set logic

Return customer_ids with at least one order in January 2026 but NONE in February 2026.

Table definitions (from the source exercise):

CREATE TABLE orders(order_id INTEGER PRIMARY KEY, customer_id INTEGER, order_date TEXT, amount REAL, rep_id INTEGER, discount REAL);
Hints
  • Distinguish a missing value from zero, an empty string and false.
  • Test comparison, aggregation and filtering on a tiny missing-value example.

Answer

Approach: January set minus February set: IN plus NOT IN (or EXISTS / NOT EXISTS).

SELECT DISTINCT customer_id
FROM orders
WHERE order_date BETWEEN '2026-01-01' AND '2026-01-31'
  AND customer_id NOT IN (
    SELECT customer_id FROM orders
    WHERE order_date BETWEEN '2026-02-01' AND '2026-02-28'
  );

The source uses SQLite with a MySQL compatibility layer. Date and string functions are dialect dependent; the query is preserved as published.

Source and licence: SQL Practice Lab — Ebrahim Sharifi · MIT

Related questions