The Named Transaction

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

Domain
SQL
Difficulty
easy
Seniority
L4
Asked in
Clear Capital

Problem

The finance team's line-item report only shows product IDs. Enrich each transaction with the product name from the catalog and walk the report down the ledger in transaction-id order, lowest first, so stakeholders can see what was actually purchased.

Transaction IDs are useless without context. Bring in the product names.

Tables

transactions

Columns: transaction_id INTEGER, user_id INTEGER, product_id INTEGER, quantity INTEGER, total_amount REAL, 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

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

Expected output

First 5 of 182 rows
transaction_iduser_idproduct_idquantitytransaction_dateproduct_name
1067197100122026-02-02Basic Device 1X
1134294105032026-03-03Deluxe Bundle 2X
1201391109942026-04-04Pro Unit 3X
1268488114852026-05-05Ultra Tool 4X
1335585119702026-06-06Essential Set 5X

Worked solution and explanation

What this really is

Strip off the finance framing and this is a fact-to-dimension lookup: each transactions row carries an opaque product_id, and the only place a human-readable name lives is the products catalog. Nothing you compute from the transaction alone produces that name, so the whole problem is the join. The trap is the two decisions wrapped around it: matching on the right key, and choosing INNER JOIN over LEFT JOIN. Match on the wrong column, or drop the condition, and you get a cartesian product that pairs every transaction with every product. Reach for LEFT JOIN and the 18 transactions whose product_id is NULL come back with a null product_name, a line a finance report cannot explain. Knowing which rows you want, and why, is the tell.


Break down the requirements

Step 1: Join transactions to products on product_id

The name lives only in products, so each transaction reaches across to the catalog on the shared product_id. That key is the primary key of products, so each transaction matches at most one catalog row and nothing fans out.

Step 2: Keep only nameable purchases

An INNER JOIN keeps a transaction only when its product_id finds a catalog row. A NULL key never equals anything, so those 18 rows fall away and the report lists 182 purchases, every one with a name.

Step 3: Project the line item and sort the ledger

Select the transaction columns plus p.product_name, leave total_amount out, and sort by transaction_id ascending so the report reads down the ledger.


The solution

Dimension lookup via inner join
SELECT
    t.transaction_id,
    t.user_id,
    t.product_id,
    t.quantity,
    t.transaction_date,
    p.product_name
FROM transactions t
INNER JOIN products p ON t.product_id = p.product_id
ORDER BY t.transaction_id;

Common follow-up questions