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.
AVG(salary) OVER (
PARTITION BY department
) AS averageSee it in action
How does each Sales employee's salary compare with their department's average?
| name | salary |
|---|---|
| Alex Reed | 33000 |
| Beth Morgan | 28000 |
| Isaac Davis | 36000 |
Before running: Will showing an average per department reduce the number of employees in the result?
SELECT name,
department,
salary,
ROUND(
AVG(salary) OVER (PARTITION BY department),
2
) AS department_average
FROM employees
WHERE department = 'Sales'
ORDER BY id;| name | department | salary | department_average |
|---|---|---|---|
| Alex Reed | Sales | 33000 | 32333.33 |
| Beth Morgan | Sales | 28000 | 32333.33 |
| Isaac Davis | Sales | 36000 | 32333.33 |
- The three Sales salaries are 33000, 28000 and 36000. Their average is 32333.33 after rounding to two decimal places.
AVG(salary) OVER (PARTITION BY department)adds that average next to each person. Three employees still appear, each with their own salary.- 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
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.
Do you want one row per employee or one per department? Here there must be one row per employee.
Use AVG(salary) with OVER (PARTITION BY department) instead of GROUP BY, so every employee is kept.
Round with ROUND(..., 2) and name the column department_average.
Solution explained
SELECT name, department, salary, ROUND(AVG(salary) OVER (PARTITION BY department), 2) AS department_average FROM employees;Each department's average is repeated next to its employees. No row disappears.
Compare salaries and positions
Guided practice
Compare salaries and positions
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.
Each employee keeps their row and needs their department's average and a position within it.
Add ROW_NUMBER() with the same partition and sort the window by salary DESC, id ASC.
Filter Marketing and Sales in WHERE and sort the final result by department, position.
Solution explained
SELECT name, department, salary,
ROUND(AVG(salary) OVER (PARTITION BY department), 2) AS department_average,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC, id ASC) AS position
FROM employees
WHERE department IN ('Marketing', 'Sales')
ORDER BY department, position;AVG as a window repeats each department's average without grouping the rows; ROW_NUMBER adds a separate position, and the outer ORDER BY sets how the result is shown.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Add the total for each customer
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.
You must keep the order detail.
The sum is calculated within a window per customer.
PARTITION BY customer_id defines which orders share the total.
Solution explained
SELECT id, customer_id, total, SUM(total) OVER (PARTITION BY customer_id) AS customer_total FROM orders;Each row is still an order. customer_total summarises all of that customer's orders and is not a running total.
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.