Skip to content
Quantoros

Using 2026-06-30 as today: return customerid and last orderdate of customers whose LAST order is more than a year old.…

SQLLevel: IntermediateDates and time

Using 2026-06-30 as today: return customer_id and last order_date of customers whose LAST order is more than a year old.

Table definitions (from the source exercise):

CREATE TABLE customers(customer_id INTEGER PRIMARY KEY, name TEXT, city TEXT, signup_date TEXT, credit_score INTEGER);
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: MAX date per customer, HAVING it before the fixed date minus 1 year.

SELECT customer_id, MAX(order_date) AS last_order
FROM orders
GROUP BY customer_id
HAVING MAX(order_date) < DATE_SUB('2026-06-30', INTERVAL 1 YEAR);

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