Bird
0
0

How can you use PARTITION BY with ROW_NUMBER() to assign unique row numbers per department but restart numbering for each department, and also order employees by hire date ascending?

hard📝 Application Q9 of 15
PostgreSQL - Window Functions in PostgreSQL
How can you use PARTITION BY with ROW_NUMBER() to assign unique row numbers per department but restart numbering for each department, and also order employees by hire date ascending?
AROW_NUMBER() OVER (ORDER BY hire_date ASC PARTITION BY department)
BROW_NUMBER() PARTITION BY department ORDER BY hire_date ASC
CROW_NUMBER() OVER (PARTITION BY department ORDER BY hire_date ASC)
DROW_NUMBER() OVER (ORDER BY hire_date ASC)
Step-by-Step Solution
Solution:
  1. Step 1: Recall correct syntax for ROW_NUMBER() with PARTITION BY

    PARTITION BY and ORDER BY must be inside OVER() with PARTITION BY first.
  2. Step 2: Identify correct option

    ROW_NUMBER() OVER (PARTITION BY department ORDER BY hire_date ASC) correctly places PARTITION BY department then ORDER BY hire_date ASC inside OVER().
  3. Final Answer:

    ROW_NUMBER() OVER (PARTITION BY department ORDER BY hire_date ASC) -> Option C
  4. Quick Check:

    PARTITION BY before ORDER BY inside OVER() for ROW_NUMBER() [OK]
Quick Trick: Inside OVER(), PARTITION BY comes before ORDER BY [OK]
Common Mistakes:
  • Placing PARTITION BY outside OVER()
  • Swapping ORDER BY and PARTITION BY order
  • Omitting OVER() clause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes