Bird
0
0

Consider the table employees with columns department and salary. What will the following query return?

medium📝 query result Q5 of 15
PostgreSQL - Window Functions in PostgreSQL
Consider the table employees with columns department and salary. What will the following query return?
SELECT employee_id, salary, SUM(salary) OVER (PARTITION BY department) AS total_dept_salary FROM employees;
AThe total salary of all employees in the table repeated on every row
BThe total salary for each department repeated on every employee row within that department
CThe cumulative salary ordered by employee_id within each department
DThe average salary per department for each employee
Step-by-Step Solution
Solution:
  1. Step 1: Understand the window function

    The query uses SUM(salary) OVER (PARTITION BY department), which calculates the sum of salaries grouped by department but does not collapse rows.
  2. Step 2: Effect on rows

    Each row will show the employee's salary and the total salary of their department repeated for all employees in that department.
  3. Final Answer:

    The total salary for each department repeated on every employee row within that department -> Option B
  4. Quick Check:

    Partition sums repeat per group [OK]
Quick Trick: SUM() OVER PARTITION repeats totals per group [OK]
Common Mistakes:
  • Confusing SUM() OVER() with aggregation collapsing rows
  • Assuming ORDER BY is required for partition sums
  • Thinking it returns cumulative sums without ORDER BY

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes