Return customerids that placed at least one order in EVERY month from January through June 2026. Table definitions…
SQLLevel: IntermediateMembership and set logic
Return customer_ids that placed at least one order in EVERY month from January through June 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
- Define one output row and the source-table keys.
- Check duplicates, NULLs and the order of filtering and aggregation.
Answer
Approach: Distinct months per customer in the window must equal 6.
SELECT customer_id
FROM orders
WHERE order_date BETWEEN '2026-01-01' AND '2026-06-30'
GROUP BY customer_id
HAVING COUNT(DISTINCT MONTH(order_date)) = 6;
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