Skip to main content
SQL • LESSON 148

Department Average Window

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

Advanced3 Minutes1610 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