Shoppers With Multiple Products in a Day
Problem
Table: `Scans`
```text
+-------------+---------+
| Column | Type |
+-------------+---------+
| product_id | int |
| brand_id | int |
| shopper_id | int |
| scan_day | date |
+-------------+---------+
There is no single-column primary key. Each row means shopper_id scanned the
product product_id on scan_day.
```
Find every shopper who scanned **more than one distinct product on the same
day**. Return one row per such shopper. Use the column heading `id` and sort
by `id` in ascending order.
**Example**
```text
Scans:
+------------+----------+------------+------------+
| product_id | brand_id | shopper_id | scan_day |
+------------+----------+------------+------------+
| 10 | 1 | 4 | 2024-04-01 |
| 20 | 2 | 4 | 2024-04-01 |
| 30 | 1 | 7 | 2024-04-01 |
| 30 | 1 | 7 | 2024-04-02 |
+------------+----------+------------+------------+
Output:
+----+
| id |
+----+
| 4 |
+----+
```
Shopper 4 scanned two different products (10 and 20) on 2024-04-01. Shopper 7
only ever scanned product 30, so they do not qualify.
Tables
Example rows — the live problem includes the full dataset.
| product_id | brand_id | shopper_id | scan_day |
|---|---|---|---|
| 10 | 1 | 4 | 2024-04-01 |
| 20 | 2 | 4 | 2024-04-01 |
| 30 | 1 | 7 | 2024-04-01 |
Expected output
Your answer should return 1 row with the columns id.
Starter code (SQL)
SELECT *
FROM Scans;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock