Skip to content
Quantoros

Return customerid and their latest orderdate using MAX (no window functions). Table definitions (from the source…

SQLLevel: IntermediateDates and time

Return customer_id and their latest order_date using MAX (no window functions).

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
  • Which rows belong to the partition and frame for each current row?
  • Check ordering, ties, frame boundaries and the difference from GROUP BY.

Answer

Approach: MAX(order_date) per customer.

SELECT customer_id, MAX(order_date) AS last_order
FROM orders
GROUP BY customer_id;

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