Skip to main content
SQL โ€ข LESSON 148

Department Average Window

How can we display each employee alongside their department average salary?

Advancedโฑ 3 Minutesโญ 1610 XP
๐Ÿค” THE QUESTION

How can we display each employee alongside their department average salary?

๐Ÿ’ก WHAT IS IT?

AVG() OVER (PARTITION BY department_id) attaches the department average to every row without GROUP BY.

๐ŸŽฏ WHAT IS IT USED FOR?

Comparing individual performance/salary against group benchmarks without reducing row count.

๐Ÿ’ป EXAMPLE
SELECT
name,
department_id,
salary,
AVG(salary) OVER (
    PARTITION BY department_id
) AS department_average
FROM employees;

๐ŸŽฏ Mission Objectives

Practice typing production-grade SQL code for Department Average Window.

  • โœ“ Window AVG
  • โœ“ PARTITION BY benchmark
  • โœ“ Non-collapsing aggregate
  • โœ“ Comparative analytics