Module 5 · Advanced queries

Add sums and averages with OVER

By the end you will be able to show a group sum or average next to each row.

About 14 min

0 of 52 lessons

Before you start, you need

Number rows with ROW_NUMBER · Summarise data with COUNT, SUM, AVG, MIN and MAX

Add the summary next to each row

You want to compare each salary with the average for its department. A summary with GROUP BY would give you one row per department, but you would lose the list of employees. An aggregate with OVER keeps its rows and adds the measure alongside.

AVG(salary) OVER (PARTITION BY department) calculates the average for employees in the same department. Each person keeps their name and salary, and people in the same group show the same average.

You can also use SUM, COUNT, MIN or MAX. For example, a sum per customer may appear next to each of their orders. The repeated figure describes the group: do not add it again as if each repetition were a new amount.

Here we calculate over the whole group, without ORDER BY within OVER. It is a total or a group average, not a running total that grows row by row. ROUND(..., 2) rounds the average to two decimal places.

Minimal syntax
AVG(salary) OVER (
  PARTITION BY department
) AS average

See it in action

How does each Sales employee's salary compare with their department's average?

Sample data: 3 rows
namesalary
Alex Reed33000
Beth Morgan28000
Isaac Davis36000

Before running: Will showing an average per department reduce the number of employees in the result?

SQL
SELECT name,
       department,
       salary,
       ROUND(
         AVG(salary) OVER (PARTITION BY department),
         2
       ) AS department_average
FROM employees
WHERE department = 'Sales'
ORDER BY id;
Example result: 3 rows
namedepartmentsalarydepartment_average
Alex ReedSales3300032333.33
Beth MorganSales2800032333.33
Isaac DavisSales3600032333.33
  1. The three Sales salaries are 33000, 28000 and 36000. Their average is 32333.33 after rounding to two decimal places.
  2. AVG(salary) OVER (PARTITION BY department) adds that average next to each person. Three employees still appear, each with their own salary.
  3. With GROUP BY there would be a single Sales row. Here OVER keeps the detail so each salary can be compared with the same group average.

Practice with help

Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.

Guided practice

Compare each salary with its group

Not started

Show name, department, salary and ROUND(AVG(salary) OVER (PARTITION BY department), 2) as department_average from employees.

Expected result: 10 rows with the columns name, department, salary, department_average, in that order. Row order does not matter.

View tables and data

Open this panel to see the full data.

Compare salaries and positions

Guided practice

Compare salaries and positions

Not started

Return name, department, salary, department_average and position for Marketing and Sales. Calculate AVG(salary) as a window per department and round it to two decimal places. Number the rows with ROW_NUMBER by salary descending, with id ascending as the tiebreaker. Sort the result by department and position.

Expected result: 5 rows with the columns name, department, salary, department_average, position, in that order. Keep the row order requested.

View tables and data

Open this panel to see the full data.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Add the total for each customer

Not started

Show id, customer_id, total and SUM(total) OVER (PARTITION BY customer_id) as customer_total from orders. Include every status.

Expected result: 18 rows with the columns id, customer_id, total, customer_total, in that order. Row order does not matter.

View tables and data

Open this panel to see the full data.

Key takeaways

  • An aggregate with OVER keeps the detail rows.
  • PARTITION BY defines the group the calculation uses.
  • The group total or average is repeated next to each row.
  • A partition total is not a running total.

Next: Practice: master window functions