Compute metrics per partition while keeping each row.
Section: Window Function Basics
Average within partition
sql
sql
SELECT employee_id, department_id, salary,
AVG(salary) OVER (PARTITION BY department_id) AS dept_avg_salary
FROM employees;Explanation
Window functions differ from GROUP BY because they keep row-level detail.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Window Function Basics