Bird
0
0

Find the issue in this query to get top 3 sales per region:

medium📝 Debug Q7 of 15
SQL - Advanced Query Patterns
Find the issue in this query to get top 3 sales per region:

SELECT region, salesperson, amount FROM sales WHERE ROW_NUMBER() OVER (PARTITION BY region ORDER BY amount DESC) <= 3;
AMissing GROUP BY clause
BROW_NUMBER() cannot be used in WHERE clause directly
CORDER BY cannot be inside ROW_NUMBER()
DPARTITION BY is invalid in this context
Step-by-Step Solution
Solution:
  1. Step 1: Understand function usage in WHERE clause

    Window functions like ROW_NUMBER() cannot be used directly in WHERE clause.
  2. Step 2: Correct usage

    Use a subquery or CTE to assign row numbers, then filter in outer query.
  3. Final Answer:

    ROW_NUMBER() cannot be used in WHERE clause directly -> Option B
  4. Quick Check:

    Window functions need subquery for filtering [OK]
Quick Trick: Use subquery to filter on ROW_NUMBER() results [OK]
Common Mistakes:
  • Using window functions directly in WHERE
  • Confusing PARTITION BY with GROUP BY
  • Ignoring need for subquery or CTE

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes