MediumSQL challenge #138SUBQUERY

Find the Amount of Each Customer's Most Recent Order

Return each user with the total of their latest order.

The challenge runs on the SQLabHub practice database (SQLite) and uses the orders table. You can solve it in the browser, with nothing to install.

Solve This SQL Challenge

The task

Find last order total for each user.

A correct answer returns 114 rows with 2 columns: user_id, total.

Tables used in this challenge

orders 200 rows

id INTEGER (PK) · user_id INTEGER · total REAL · discount REAL · item_count INTEGER · status TEXT · order_date TEXT · shipped_date TEXT · shipping_city TEXT · shipping_country TEXT

First 3 rows of the orders table
iduser_idtotaldiscountitem_countstatusorder_dateshipped_dateshipping_cityshipping_country
1432638.110.253cancelled2024-04-24NULLLos AngelesUSA
2829470.195delivered2024-03-152023-04-09ChicagoUSA
3136683.240.082delivered2023-02-152023-07-10Los AngelesUSA

The first rows of each table are shown. See how all the tables relate on the database schema page.

Expected output

Expected output of the challenge
user_idtotal
432638.11
136683.24
1401120.99
501327.01
914163.77

First 5 of 114 rows. This is the output of the model solution on the practice database. The task does not fix a sort order, so your rows may come back in a different order.

What you practise

  • Choosing exactly which columns a query returns
  • Filtering rows with a WHERE condition
  • Summarising many rows into one value with COUNT, SUM, AVG, MIN or MAX
  • Writing a subquery that refers to the current row of the outer query

Hint and approach

Try the challenge first. Open these only when you need them.

Show the hint

MAX(order_date)

Show the step-by-step approach

How the model solution is built, in the order the database evaluates it:

  1. Read from the orders table.
  2. Filter in WHERE: keep the rows where order_date equals the value returned by a subquery.
  3. The subquery is correlated: it refers to the current outer row, so it is evaluated for each row. It works like this:
    1. Read from the orders table.
    2. Filter in WHERE: keep the rows where user_id equals the outer row's user_id.
    3. Aggregate over all the rows that are left: compute MAX(order_date). With no GROUP BY, the result is a single row.
  4. Return only the columns user_id and total.

Model solution

Checking your session…

What to watch for

A customer with two orders on their latest date gets two rows, because the match is on the date. The table has no time of day to break the tie.

  • Correlated subquery. A correlated subquery is evaluated per outer row, which gets slow on large tables. A join to a grouped subquery or a window function usually does the same work in one pass.
  • Aggregate functions. Aggregates skip NULLs. AVG divides by the number of non-NULL values and COUNT(column) can be smaller than COUNT(*).
  • WHERE. WHERE is evaluated before the SELECT list, so it cannot use a column alias defined there or the result of an aggregate.
Solve This SQL Challenge

Keep learning