Window Functions · SQL SQL

Consider the following query: SELECT employeeid, departmentid, salary, ROWNUMBER() OVER (PARTITION BY departmentid ORDER BY salary DESC) as rn FROM e…

Practice question from Window Functions (SQL SQL), with the correct answer and a step-by-step solution.

easy · MCQ · Q-2841D6

Consider the following query: SELECT employee_id, department_id, salary, ROW_NUMBER() OVER (PARTITION BY department_id ORDER BY salary DESC) as rn FROM employees;. What does the PARTITION BY clause do?

  1. A.It sorts the entire result set by department_id before applying the window function.
  2. B.It divides the result set into subsets or partitions, and the window function is applied independently to each partition.Correct answer
  3. C.It filters out rows that do not match the specified department_id.
  4. D.It limits the number of rows returned per department to a single row.

Solution

The PARTITION BY clause divides the rows of the result set into logical groups (partitions). The window function computation is then reset and evaluated independently within each partition.

More from Window Functions

All of Window Functions

Practise more SQL

Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.

Start practising free

Free to start · No credit card needed