Find the products with the highest total quantity purchased, including ties

Quick Overview

Write a SQL query that finds the products with the highest total purchased quantity across all orders and returns every product tied for the top. It tests joins, grouped sums, and comparing against an aggregate so that ties are kept.

Find the products with the highest total quantity purchased, including ties

Company: Intel

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Find the product or products with the greatest sum of `quantity` across all purchases. When several products share the top total, return all of them. Return `product_id`, `product_name` and `total_purchased`, ordered by `product_id` ascending. ```hint Watch the ties Picture two products sharing the top total. Check that your query would return both of them. ``` ### Input Table(s) `Products` | Column | Type | Meaning | |---|---|---| | product_id | INT | Unique id of the product | | product_name | VARCHAR(100) | Product name, NOT NULL | | category | VARCHAR(50) | Product category, may be NULL | | price | DECIMAL(10,2) | Unit price, may be NULL | Primary key: `product_id`. `Purchases` | Column | Type | Meaning | |---|---|---| | purchase_id | INT | Unique id of the purchase | | product_id | INT | Product bought, references `Products.product_id` | | quantity | INT | Units bought in this purchase, NOT NULL | | purchase_date | DATE | Date of the purchase, NOT NULL | Primary key: `purchase_id`. Foreign key: `product_id` references `Products(product_id)`. ### Output Contract - Columns, in this order: `product_id`, `product_name`, `total_purchased`. - `total_purchased` is the integer sum of `quantity` over all of the product's purchases. No rounding applies. - One row for each product whose total equals the highest total, so ties all appear. - Rows ordered by `product_id` ascending. `product_id` is unique, so the order is fully determined. ### Example `Products` | product_id | product_name | category | price | |---|---|---|---| | 1 | Laptop | Electronics | 899.00 | | 2 | Monitor | Electronics | 199.99 | | 3 | Keyboard | Accessories | 49.50 | | 4 | Webcam | NULL | NULL | `Purchases` | purchase_id | product_id | quantity | purchase_date | |---|---|---|---| | 101 | 1 | 3 | 2024-01-05 | | 102 | 2 | 5 | 2024-01-06 | | 103 | 1 | 2 | 2024-01-07 | | 104 | 3 | 1 | 2024-01-08 | | 105 | 3 | 2 | 2024-01-09 | Expected result: | product_id | product_name | total_purchased | |---|---|---| | 1 | Laptop | 5 | | 2 | Monitor | 5 | Laptop totals 3 + 2 = 5 and Monitor totals 5, so they tie for the top. Keyboard totals 3, and Webcam has no purchases. ### Constraints and Clarifications - `Products` has at most 10,000 rows and `Purchases` at most 100,000 rows. - `quantity` is at least 1. - Every `product_id` in `Purchases` exists in `Products`, so it is never NULL. - There is at least one purchase record, so the highest total is at least 1 and a product with no purchases can never be in the result. - `category` and `price` can be NULL. Neither affects the answer. - `product_name` has no uniqueness constraint. Two products with the same name are still separate products, identified by `product_id`.

Overview: Write a SQL query that finds the products with the highest total purchased quantity across all orders and returns every product tied for the top. It tests joins, grouped sums, and comparing against an aggregate so that ties are kept.

Intel logo
Intel
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Find the product or products with the greatest sum of quantity across all purchases. When several products share the top total, return all of them. Return product_id, product_name and total_purchased, ordered by product_id ascending.

Input Table(s)

Products

ColumnTypeMeaning
product_idINTUnique id of the product
product_nameVARCHAR(100)Product name, NOT NULL
categoryVARCHAR(50)Product category, may be NULL
priceDECIMAL(10,2)Unit price, may be NULL

Primary key: product_id.

Purchases

ColumnTypeMeaning
purchase_idINTUnique id of the purchase
product_idINTProduct bought, references Products.product_id
quantityINTUnits bought in this purchase, NOT NULL
purchase_dateDATEDate of the purchase, NOT NULL

Primary key: purchase_id. Foreign key: product_id references Products(product_id).

Output Contract

  • Columns, in this order: product_id , product_name , total_purchased .
  • total_purchased is the integer sum of quantity over all of the product's purchases. No rounding applies.
  • One row for each product whose total equals the highest total, so ties all appear.
  • Rows ordered by product_id ascending. product_id is unique, so the order is fully determined.

Example

Products

product_idproduct_namecategoryprice
1LaptopElectronics899.00
2MonitorElectronics199.99
3KeyboardAccessories49.50
4WebcamNULLNULL

Purchases

purchase_idproduct_idquantitypurchase_date
101132024-01-05
102252024-01-06
103122024-01-07
104312024-01-08
105322024-01-09

Expected result:

product_idproduct_nametotal_purchased
1Laptop5
2Monitor5

Laptop totals 3 + 2 = 5 and Monitor totals 5, so they tie for the top. Keyboard totals 3, and Webcam has no purchases.

Constraints and Clarifications

  • Products has at most 10,000 rows and Purchases at most 100,000 rows.
  • quantity is at least 1.
  • Every product_id in Purchases exists in Products , so it is never NULL.
  • There is at least one purchase record, so the highest total is at least 1 and a product with no purchases can never be in the result.
  • category and price can be NULL. Neither affects the answer.
  • product_name has no uniqueness constraint. Two products with the same name are still separate products, identified by product_id .