Bird
0
0

Given the query:

medium📝 query result Q13 of 15
PostgreSQL - Window Functions in PostgreSQL
Given the query:
SELECT department, employee, salary, AVG(salary) OVER w AS avg_salary
FROM employees
WINDOW w AS (PARTITION BY department ORDER BY employee)
ORDER BY department, employee;

What does the avg_salary column show?
AThe average salary of employees in the same department up to the current employee
BThe average salary of all employees in the company
CThe average salary of employees partitioned by department without order
DThe average salary of employees ordered by employee name across all departments
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the WINDOW clause

    The window named w partitions rows by department and orders by employee.
  2. Step 2: Understand AVG() OVER w behavior

    AVG(salary) OVER w calculates the average salary for rows in the same department up to the current employee in order.
  3. Final Answer:

    The average salary of employees in the same department up to the current employee -> Option A
  4. Quick Check:

    Partition + order = running average per department [OK]
Quick Trick: Partition + order means running calculation per group [OK]
Common Mistakes:
  • Ignoring ORDER BY in window
  • Assuming average over entire table
  • Confusing partition with no partition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes