Bird
0
0

Given the table Employees with columns Department and Salary, what will the following query return?

medium📝 query result Q13 of 15
SQL - Window Functions Fundamentals
Given the table Employees with columns Department and Salary, what will the following query return?
SELECT Department, Salary, RANK() OVER (PARTITION BY Department ORDER BY Salary DESC) AS SalaryRank FROM Employees;
AReturns the salary and department without any ranking.
BRanks salaries across all employees ignoring departments.
CCalculates the total salary per department.
DRanks salaries within each department from highest to lowest, starting at 1 for the highest salary.
Step-by-Step Solution
Solution:
  1. Step 1: Understand RANK() with PARTITION BY

    The RANK() function assigns ranks starting at 1 within each partition (department), ordered by salary descending.
  2. Step 2: Analyze the query output

    Each employee gets a rank based on salary within their department, highest salary ranked 1, ties get same rank.
  3. Final Answer:

    Ranks salaries within each department from highest to lowest, starting at 1 for the highest salary. -> Option D
  4. Quick Check:

    RANK() OVER PARTITION BY groups and ranks rows [OK]
Quick Trick: RANK() with PARTITION BY ranks within groups, not whole table [OK]
Common Mistakes:
  • Thinking RANK() ignores PARTITION BY
  • Confusing RANK() with SUM()
  • Assuming ranking is global, not per group

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes