ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Find Customers with Most Recent Order

SQL

Goal -- WPM

Ready
Exercise Algorithm Area
1WITH OrderDates AS (
2SELECT
3CustomerID,
4OrderID,
5OrderDate,
6ROW_NUMBER() OVER (ORDER BY OrderDate DESC, OrderID DESC) as rn
7FROM
8Orders
9WHERE
10OrderDate IS NOT NULL
11),
12MaxOrderDate AS (
13SELECT
14MAX(OrderDate) as MaxDate
15FROM
16Orders
17WHERE
18OrderDate IS NOT NULL
19)
20SELECT
21o.CustomerID,
22o.OrderID,
23o.OrderDate
24FROM
25Orders o
26JOIN
27MaxOrderDate mod ON o.OrderDate = mod.MaxDate
28WHERE
29o.OrderDate IS NOT NULL;
30
31-- Alternative using RANK() window function
32WITH RankedOrders AS (
33SELECT
34CustomerID,
35OrderID,
36OrderDate,
37RANK() OVER (ORDER BY OrderDate DESC, OrderID DESC) as rnk
38FROM
39Orders
40WHERE
41OrderDate IS NOT NULL
42)
43SELECT
44CustomerID,
45OrderID,
46OrderDate
47FROM
48RankedOrders
49WHERE
50rnk = 1;
51
52-- Helper function concept: find_max_date
53-- The MAX() aggregate function serves this purpose.
54
55-- Edge case: Empty Orders table
56-- If the Orders table is empty, both approaches will return an empty result set, which is correct.
57
58-- Edge case: NULL OrderDate
59-- Rows with NULL OrderDate are excluded by the WHERE clause in both approaches.
60
61-- Edge case: Multiple orders on the same most recent date
62-- The JOIN approach with MaxOrderDate correctly includes all orders matching the MaxDate.
63-- The RANK() approach also correctly includes all tied orders because RANK() assigns the same rank to ties.
64
65-- Invariant: The returned rows represent all orders that share the absolute latest OrderDate in the table.
66
67-- Correctness argument:
68-- The first approach finds the absolute maximum OrderDate across all orders and then selects all orders that match this maximum date. This ensures all ties are captured.
69-- The second approach uses RANK() to assign a rank based on descending OrderDate. Orders with the same latest date receive the same rank (1). Selecting rows where rank is 1 correctly retrieves all the most recent orders, including ties. Both methods are robust and handle edge cases appropriately.
Algorithm description viewbox

Find Customers with Most Recent Order

Algorithm description:

This SQL query identifies the customer(s) who placed the most recent order, including their order details. This is vital for understanding current customer activity, identifying high-value recent transactions, and informing time-sensitive marketing campaigns. The query efficiently finds the latest order date and retrieves all associated orders, handling ties correctly, making it a core analytical tool for sales and marketing.

Algorithm explanation:

The algorithm finds the customer(s) with the most recent order. It employs two primary strategies. The first involves finding the maximum `OrderDate` across all orders (excluding NULLs) using a subquery or CTE (`MaxOrderDate`), and then joining the `Orders` table back to this maximum date to retrieve all orders matching that date. This naturally handles ties. The second strategy uses the `RANK()` window function, partitioning by nothing (effectively ordering the entire table) and ordering by `OrderDate` in descending order. `RANK()` assigns the same rank to orders with identical dates. Selecting rows where the rank is 1 retrieves all the most recent orders, including ties. Both methods are efficient, with time complexity typically O(N log N) due to sorting for window functions or O(N) if indexes are leveraged effectively, where N is the number of orders. Space complexity is O(N) for intermediate results.

Pseudocode:

1. Find the latest order date in the entire orders table.
2. Select all orders that have this latest order date.
3. Include the customer ID and order details for these selected orders.
4. If multiple orders share the same latest date, include all of them.