Bird
0
0

Which window function with PARTITION BY should you use?

hard📝 Application Q8 of 15
SQL - Window Functions Fundamentals
You want to assign a sequential rank to salespeople within each region, but if two salespeople have the same sales amount, they should get different ranks (no ties). Which window function with PARTITION BY should you use?
ADENSE_RANK() OVER (PARTITION BY Region ORDER BY SalesAmount DESC)
BROW_NUMBER() OVER (PARTITION BY Region ORDER BY SalesAmount DESC)
CRANK() OVER (PARTITION BY Region ORDER BY SalesAmount DESC)
DSUM(SalesAmount) OVER (PARTITION BY Region)
Step-by-Step Solution
Solution:
  1. Step 1: Understand difference between ROW_NUMBER, RANK, and DENSE_RANK

    ROW_NUMBER assigns unique sequential numbers without ties; RANK and DENSE_RANK assign same rank to ties.
  2. Step 2: Choose function that assigns unique ranks per region

    ROW_NUMBER() OVER (PARTITION BY Region ORDER BY SalesAmount DESC) fits requirement.
  3. Final Answer:

    ROW_NUMBER() OVER (PARTITION BY Region ORDER BY SalesAmount DESC) -> Option B
  4. Quick Check:

    ROW_NUMBER() = unique ranks, no ties [OK]
Quick Trick: Use ROW_NUMBER() for unique ranks, RANK() allows ties [OK]
Common Mistakes:
  • Using RANK() or DENSE_RANK() when unique ranks needed
  • Confusing SUM() with ranking functions
  • Omitting PARTITION BY for per-group ranking

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes