SOL-C01 · Question #88
You have a table named 'PRODUCT SALES' with columns 'product id', 'sale date', 'region', and 'sales_amount'. You need to create a query that returns the top 3 products by total sales amount for each r
The correct answer is E. Option E. The actual SQL code for options A–E wasn't included in your message - only the letter labels were provided. Without seeing the query text, I can't explain specifically why each distractor fails. That said, here's what you need to know to answer this type of question correctly on
Question
You have a table named 'PRODUCT SALES' with columns 'product id', 'sale date', 'region', and 'sales_amount'. You need to create a query that returns the top 3 products by total sales amount for each region. Which of the following SQL queries using window functions correctly implements this requirement?
Exhibit
Options
- AOption A
- BOption B
- COption C
- DOption D
- EOption E
How the community answered
(43 responses)- A19% (8)
- B7% (3)
- C5% (2)
- D2% (1)
- E67% (29)
Explanation
The actual SQL code for options A–E wasn't included in your message - only the letter labels were provided. Without seeing the query text, I can't explain specifically why each distractor fails.
That said, here's what you need to know to answer this type of question correctly on any exam:
What a correct answer must do:
- Aggregate first -
SUM(sales_amount)grouped byproduct_idandregion - Rank within each region - using
RANK()orROW_NUMBER()withPARTITION BY region ORDER BY total_sales DESC - Filter to top 3 - wrap in a CTE or subquery, then
WHERE rank <= 3(you can't filter on a window function alias in the sameWHEREclause)
WITH ranked AS (
SELECT product_id, region,
SUM(sales_amount) AS total_sales,
RANK() OVER (PARTITION BY region ORDER BY SUM(sales_amount) DESC) AS rnk
FROM "PRODUCT SALES"
GROUP BY product_id, region
)
SELECT product_id, region, total_sales
FROM ranked
WHERE rnk <= 3;
Common distractor patterns to watch for:
- Missing
PARTITION BY(ranks globally, not per region) - Missing
GROUP BYbefore the window function (no aggregation) - Filtering
WHERE rank <= 3in the same query level where rank is defined (not allowed) - Using
NTILE(3)instead ofRANK()(divides into thirds, doesn't return "top 3")
Memory tip: Think "CTE → aggregate → partition → filter" - you always need a wrapper query to filter on a window function result.
If you paste the actual query options, I can give you a precise breakdown of each.
Topics
Community Discussion
No community discussion yet for this question.
