The Final Sale

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L5
Asked in
Databricks

Problem

An analyst reviewing the catalog dashboard found sale figures that matched no single sale, when only each product's most recent transaction in transactions belongs on the page and products that never sold have nothing to show. List those products alphabetically by name from products, with each one's category and the amount and date of its latest sale.

Every product has a last sale. When was it?

Tables

products

Columns: product_id INTEGER, product_name TEXT, category TEXT, price REAL, rating REAL, in_stock INTEGER

products: 10 of 200 rows
product_idproduct_namecategorypriceratingin_stock
1001Basic Device 1XBooks17.522.71
1050Deluxe Bundle 2XClothing25.054.41
1099Pro Unit 3XHome & Kitchen32.582.11
1148Ultra Tool 4XCollectibles40.113.81
1197Essential Set 5XToys47.641.51
1246Classic System 6XBeauty03.21
1295Smart Gadget 7XAutomotive62.74.90
1344Eco Kit 8XGarden70.232.61
1393Turbo Pack 9XMusic77.764.30
1442Premium Widget 10XElectronics85.2921

transactions

Columns: transaction_id INTEGER, product_id INTEGER, user_id INTEGER, total_amount REAL, quantity INTEGER, transaction_date TEXT

transactions: 10 of 200 rows
transaction_iduser_idproduct_idquantitytotal_amounttransaction_date
10671971001223.462026-02-02
11342941050336.932026-03-03
12013911099450.42026-04-04
12684881148563.872026-05-05
13355851197002026-06-06
14026821246290.812026-07-07
146977912953104.282026-08-08
153687613444117.752026-09-09
160397313935-452026-01-10
1670107014421144.692026-02-11

Expected output

First 5 of 91 rows
product_namecategorylatest_sale_amountlast_sale_date
Basic Device 1XBooks225.512026-08-17
Basic Device 21XBooks292.862026-04-22
Basic Device 31XBooks427.562026-05-04
Basic Device 41XBooks562.262026-06-14
Basic Device 51XBooks696.962026-07-24

Worked solution and explanation

What this really is

This is greatest-one-per-group dressed up as a dashboard fix. The skill is keeping the amount and the date from the same physical row. The tempting GROUP BY product_id with MAX(transaction_date) and MAX(total_amount) computes each column on its own, so it can stamp a product's biggest-ever sale with its newest date: two transactions fused into one that never happened. Joining back on the max date is the other reflex, and it emits a product twice whenever two sales share that date.


Break down the requirements

Step 1: Rank transactions inside each product

Partition transactions by product_id and order newest-first on transaction_date. Break date ties on transaction_id descending, because IDs climb with each sale, so the pick stays deterministic when two sales land on the same day.

Step 2: Keep only the latest row

Filter to rn = 1. That leaves exactly one row per product, and its total_amount and transaction_date belong to the same sale.

Step 3: Attach product metadata with an inner join

Join the latest rows to products on product_id to pick up product_name and category. The inner join is what keeps never-sold products off the page. The null-product transactions also fall away here, because they match no product.

Step 4: Sort alphabetically by name

Order by product_name alone. Names are unique in products, so the alphabetical order is already settled and a second key would decide nothing.


The solution

Window function for latest-per-group
WITH ranked AS (
    SELECT product_id,
           total_amount,
           transaction_date,
           ROW_NUMBER() OVER (
               PARTITION BY product_id
               ORDER BY transaction_date DESC, transaction_id DESC
           ) AS rn
    FROM transactions
)
SELECT p.product_name,
       p.category,
       r.total_amount     AS latest_sale_amount,
       r.transaction_date AS last_sale_date
FROM products p
JOIN ranked r ON r.product_id = p.product_id
WHERE r.rn = 1
ORDER BY p.product_name

Common follow-up questions