Above Average

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
Tata Consultancy Services

Problem

The pricing team is identifying premium products that stand out from the rest of the catalog. Show the name, category, and price of every product whose price exceeds the overall catalog average, and include a column with that catalog-wide average for reference. Present results from most expensive to least.

Products beating the catalog average.

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

Expected output

First 5 of 96 rows
product_namecategorypricecatalog_avg
Basic Set v100Electronics1859.72896.6304188481676
Premium Widget v99Music1850.45896.6304188481676
Turbo Set v98Garden1841.18896.6304188481676
Eco Widget v97Automotive1831.91896.6304188481676
Smart Set v96Beauty1822.64896.6304188481676

Worked solution and explanation

Why this problem exists in real interviews

This probes scalar subquery filtering, combined with aggregation. Getting it right quickly signals comfort with the building blocks that complex queries depend on.


Break down the requirements

Step 1: Compute the benchmark average of price

Compute the catalog-wide average price with a scalar subquery (SELECT AVG(price) FROM products).

Step 2: Filter rows exceeding the benchmark

Filter products to those whose price exceeds that average via a correlated-free scalar subquery in WHERE.

Step 3: Order by the comparison column

Project product_name, category, price, and the same average as a reference column catalog_avg.


The solution

Scalar subquery with average reference column
SELECT product_name, category, price, (SELECT AVG(price) FROM products) AS catalog_avg FROM products WHERE price > (SELECT AVG(price) FROM products) ORDER BY price DESC

Common follow-up questions