Top 3 Product Categories by Average Price of Active Products
Quick Overview
Write a SQL query that averages product prices per category using only active products and returns the three categories with the highest average, rounded to two decimals. It tests filtering before grouping, AVG with ROUND, and an alphabetical tie-break under a row limit.
Top 3 Product Categories by Average Price of Active Products
Company: Samsung
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Find the top 3 categories by average price of products with `status = 'active'`. Return `category` and `avg_price` (rounded to 2 decimals). Order by `avg_price` descending, with ties broken by `category` ascending, and return at most 3 rows.
```hint Which average gets compared
Filter the rows before you group, and decide which value the tie rule compares once the average has been rounded.
```
### Input Table(s)
`products` (primary key `product_id`)
| column | type | meaning |
|---|---|---|
| product_id | INT | Unique product identifier |
| product_name | VARCHAR(100) | Product name |
| category | VARCHAR(50) | Product category, never NULL |
| price | DECIMAL(10,2) | Product price, non-negative |
| status | VARCHAR(20) | `'active'` or `'inactive'` |
### Output Contract
- Columns, in this order: `category`, `avg_price` (average `price` of the category's active products, rounded to 2 decimal places).
- Order by `avg_price` descending, then `category` ascending. The ordering uses the rounded `avg_price`, so two categories that round to the same value tie and are ordered by name.
- Return at most 3 rows.
### Example
`products`
| product_id | product_name | category | price | status |
|---|---|---|---|---|
| 1 | Phone A | phones | 900.00 | active |
| 2 | Phone B | phones | 500.00 | active |
| 3 | Phone C | phones | 2000.00 | inactive |
| 4 | TV A | tvs | 1199.99 | active |
| 5 | TV B | tvs | 600.00 | active |
| 6 | TV C | tvs | 900.00 | active |
| 7 | Laptop A | laptops | 1000.00 | active |
| 8 | Laptop B | laptops | 800.00 | active |
| 9 | Laptop C | laptops | 300.00 | active |
| 10 | Tablet A | tablets | 700.00 | active |
| 11 | Tablet B | tablets | 700.00 | active |
| 12 | Tablet C | tablets | 700.01 | active |
| 13 | Earbuds A | audio | 150.00 | active |
| 14 | Speaker A | audio | 250.00 | active |
Expected result
| category | avg_price |
|---|---|
| tvs | 900.00 |
| laptops | 700.00 |
| phones | 700.00 |
The inactive Phone C is ignored, so phones averages (900.00 + 500.00) / 2 = 700.00. tvs averages 2699.99 / 3 = 899.9967, which rounds to 900.00. laptops, phones and tablets all round to 700.00 (tablets is 700.0033 before rounding), so the name order puts laptops and phones in the last two places and leaves tablets out.
### Constraints and Clarifications
- The table has between 1 and 10,000 products.
- `price` is non-negative and `category` is never NULL.
- `status` is either `'active'` or `'inactive'`; only active products count toward an average.
- Each product counts once in its category's average, even when several products share a price or a name.
- A category with no active products does not appear. If fewer than 3 categories have active products, return fewer rows.
Overview: Write a SQL query that averages product prices per category using only active products and returns the three categories with the highest average, rounded to two decimals. It tests filtering before grouping, AVG with ROUND, and an alphabetical tie-break under a row limit.
Find the top 3 categories by average price of products with status = 'active'. Return category and avg_price (rounded to 2 decimals). Order by avg_price descending, with ties broken by category ascending, and return at most 3 rows.
Input Table(s)
products (primary key product_id)
column
type
meaning
product_id
INT
Unique product identifier
product_name
VARCHAR(100)
Product name
category
VARCHAR(50)
Product category, never NULL
price
DECIMAL(10,2)
Product price, non-negative
status
VARCHAR(20)
'active' or 'inactive'
Output Contract
Columns, in this order:
category
,
avg_price
(average
price
of the category's active products, rounded to 2 decimal places).
Order by
avg_price
descending, then
category
ascending. The ordering uses the rounded
avg_price
, so two categories that round to the same value tie and are ordered by name.
Return at most 3 rows.
Example
products
product_id
product_name
category
price
status
1
Phone A
phones
900.00
active
2
Phone B
phones
500.00
active
3
Phone C
phones
2000.00
inactive
4
TV A
tvs
1199.99
active
5
TV B
tvs
600.00
active
6
TV C
tvs
900.00
active
7
Laptop A
laptops
1000.00
active
8
Laptop B
laptops
800.00
active
9
Laptop C
laptops
300.00
active
10
Tablet A
tablets
700.00
active
11
Tablet B
tablets
700.00
active
12
Tablet C
tablets
700.01
active
13
Earbuds A
audio
150.00
active
14
Speaker A
audio
250.00
active
Expected result
category
avg_price
tvs
900.00
laptops
700.00
phones
700.00
The inactive Phone C is ignored, so phones averages (900.00 + 500.00) / 2 = 700.00. tvs averages 2699.99 / 3 = 899.9967, which rounds to 900.00. laptops, phones and tablets all round to 700.00 (tablets is 700.0033 before rounding), so the name order puts laptops and phones in the last two places and leaves tablets out.
Constraints and Clarifications
The table has between 1 and 10,000 products.
price
is non-negative and
category
is never NULL.
status
is either
'active'
or
'inactive'
; only active products count toward an average.
Each product counts once in its category's average, even when several products share a price or a name.
A category with no active products does not appear. If fewer than 3 categories have active products, return fewer rows.