Bird
0
0

Given the table employees with columns department and salary, what will this query return?

medium📝 query result Q13 of 15
PostgreSQL - Window Functions in PostgreSQL
Given the table employees with columns department and salary, what will this query return?
SELECT department, salary, RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rank FROM employees;
ARanks employees within each department by salary, highest salary gets rank 1.
BRanks all employees together ignoring department, highest salary gets rank 1.
CCalculates total salary per department.
DReturns an error because RANK() cannot be used with PARTITION BY.
Step-by-Step Solution
Solution:
  1. Step 1: Understand RANK() with PARTITION BY

    RANK() assigns ranks within each partition (department) ordered by salary descending.
  2. Step 2: Analyze query output

    Each employee gets a rank starting at 1 for highest salary in their department, ties get same rank.
  3. Final Answer:

    Ranks employees within each department by salary, highest salary gets rank 1. -> Option A
  4. Quick Check:

    RANK() with PARTITION BY ranks within groups [OK]
Quick Trick: RANK() with PARTITION BY ranks within groups [OK]
Common Mistakes:
  • Thinking RANK() ignores PARTITION BY
  • Confusing RANK() with aggregation
  • Assuming RANK() causes errors with PARTITION BY

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes