Top Products by Quantity Sold

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
ESPN

Problem

An analyst closing the 2026 books needs the units sold per product from the transactions table, with sales logged without a product kept together on a single line. List each product with its total quantity, highest volume first.

The bestsellers. By volume.

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

Expected output

First 5 of 90 rows
product_idtotal_quantity
NULL30
10506
55585
53135
50685

Worked solution and explanation

A sum dressed up as a leaderboard

This is a sum over a measure dressed up as a sales leaderboard. Filtering to 2026 and sorting is the easy half. What separates candidates is reading 'total quantity sold' as units, SUM(quantity), instead of counting the rows of transactions. Each sale carries 0 to 5 units, so COUNT(*) quietly turns a volume board into a frequency board: a voided 0-unit sale scores 1, a 5-unit sale scores 1, and the order scrambles. The second tell is the 30 units sold without a product_id. They belong on the board as one line, and they top it.

Step 1: Scope the sales to 2026

transaction_date is text in 'YYYY-MM-DD' form, and the table holds sales from 2022 on. Pull out the year and keep only '2026'. Leave the filter off and earlier years pile onto the same products, so every total inflates.

Step 2: Add up quantity per product_id

One output row per product, and the number on it is units: SUM(quantity) AS total_quantity. A product with two 3-unit sales has moved 6 units, while COUNT(*) would call it 2. Name the column for what it is; that name is half the answer's contract.

Step 3: Keep the sales with no product

GROUP BY product_id collects every NULL key into a single group, which is exactly the line the ask wants. Do not add WHERE product_id IS NOT NULL to tidy up, because that group is the largest on the board.

Step 4: Sort by volume, highest first

ORDER BY total_quantity DESC puts the biggest sellers on top. Plenty of products land on the same total, but the ask fixes only the volume order, so tied products may come back in any order and no tiebreaker is owed.

Units sold per product, highest volume first
SELECT
  product_id,
  SUM(quantity) AS total_quantity
FROM transactions
WHERE strftime('%Y', transaction_date) = '2026'
GROUP BY product_id
ORDER BY total_quantity DESC
Counting sales
COUNT(*) per product_id measures how often a product sold. The voided sale 1335 of product 1197 records quantity 0 and still scores 1.
Adding up units
SUM(quantity) measures how much moved. That voided sale adds 0, and a 5-unit sale counts five times as much as a 1-unit one.

Common follow-up questions