Bird
0
0

What does the ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) window function do in a query?

easy📝 Conceptual Q11 of 15
PostgreSQL - Window Functions in PostgreSQL
What does the ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) window function do in a query?
ACounts the number of employees in each department.
BAssigns a unique rank to each employee within their department based on salary, starting at 1 for the highest salary.
CCalculates the total salary for each department.
DReturns the average salary of all employees.
Step-by-Step Solution
Solution:
  1. Step 1: Understand ROW_NUMBER() with PARTITION BY

    The function assigns a unique sequential number to rows within each department group.
  2. Step 2: ORDER BY salary DESC ranks highest salary first

    Ordering by salary descending means the highest salary gets row number 1 in each department.
  3. Final Answer:

    Assigns a unique rank to each employee within their department based on salary, starting at 1 for the highest salary. -> Option B
  4. Quick Check:

    ROW_NUMBER() with PARTITION BY and ORDER BY = ranked rows [OK]
Quick Trick: ROW_NUMBER() numbers rows per group ordered by criteria [OK]
Common Mistakes:
  • Confusing ROW_NUMBER() with COUNT() or SUM()
  • Ignoring PARTITION BY and thinking it ranks globally
  • Assuming it calculates totals or averages

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes