Present and Accounted For

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
L3
Asked in
DoorDash

Problem

The merchandising team wants a per-product read on how much sales volume comes specifically from the 'Electronics' category. For every product, show the total transaction amount tied to Electronics, biggest first, and keep products that have never sold under Electronics in the list with a zero.

Every product lands on the report, even the ones Electronics never touched.

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, total_amount REAL, user_id INTEGER, 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 200 rows
product_nameelectronics_total
Premium Widget 100X2713.98
Premium Widget 90X2444.58
Premium Widget 80X2175.18
Premium Widget 70X1905.78
Premium Widget 60X1636.38

Worked solution and explanation

What this problem really is

This is a conditional sum wearing a join costume. The real question: can you total each product's Electronics volume while still listing the products that sold zero Electronics? Anyone can sum the amounts. The trap is where the category filter lives. Push 'category = Electronics' into a WHERE clause and your LEFT JOIN silently collapses to an INNER JOIN, and every product that never sold an Electronics item vanishes from the report the business explicitly asked to see in full.


Break down the requirements

Step 1: Start from all products

LEFT JOIN products to transactions on product_id, driving from products so every product survives, even the ones with no transactions at all.

Step 2: Filter inside the aggregate, not in WHERE

Keep the category test inside the aggregate: SUM(CASE WHEN p.category = 'Electronics' THEN t.total_amount END). Non-Electronics rows contribute nothing to the sum, but the product row itself is never dropped.

Step 3: Zero-fill the empty case

A product with no Electronics volume produces a NULL sum. Wrap it in COALESCE(..., 0) so the report reads 0 for those products instead of a blank.


The solution

Left join with a filtered condition and zero-fill
SELECT p.product_name,
       COALESCE(SUM(CASE WHEN p.category = 'Electronics' THEN t.total_amount END), 0) AS electronics_total
FROM products p
LEFT JOIN transactions t ON p.product_id = t.product_id
GROUP BY p.product_id, p.product_name
ORDER BY electronics_total DESC, p.product_name
Filter in the aggregate (correct)
SUM(CASE WHEN p.category = 'Electronics' THEN t.total_amount END) leaves every product row intact and simply contributes 0 for the non-Electronics ones. All products stay in the output.
Filter in WHERE (drops products)
WHERE p.category = 'Electronics' evaluates after the LEFT JOIN and discards every row where the match is absent, turning the outer join into an inner one. Products with no Electronics volume disappear entirely.

Common follow-up questions