Bird
0
0

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

medium📝 query result Q4 of 15
SQL - Window Functions Fundamentals
Given the table employees with columns id, department, and salary, what will the query below return?
SELECT id, salary, RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rank FROM employees;
ARanks all employees ignoring department, ordered by salary ascending
BCalculates the total salary per department
CAssigns a unique row number to each employee ordered by salary descending
DRanks employees within each department by descending salary, with ties sharing the same rank
Step-by-Step Solution
Solution:
  1. Step 1: Understand PARTITION BY usage

    PARTITION BY department divides rows into groups by department.
  2. Step 2: Understand RANK() with ORDER BY salary DESC

    Within each department, RANK() orders salaries descending and assigns ranks; ties get same rank.
  3. Final Answer:

    Ranks employees within each department by descending salary, with ties sharing the same rank -> Option D
  4. Quick Check:

    RANK() + PARTITION BY = rank per group with ties [OK]
Quick Trick: RANK() with PARTITION BY ranks within groups, ties share rank [OK]
Common Mistakes:
  • Ignoring PARTITION BY and ranking all rows together
  • Confusing RANK() with ROW_NUMBER()
  • Assuming ascending order when DESC is used

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes