Window Functions in SQL Server: Simple Examples
08a - WINDOW FUNCTIONS (VERY SIMPLE version)
---------------------------------------------------------------------
No setup needed. This file creates its own small table.
A window function adds a calculation to EVERY row,
without combining the rows like GROUP BY does.
SUM(column) OVER () -> total of all rows
SUM(column) OVER (PARTITION BY Department) -> total per department
Run the sample database script first. Every query below was run on that data, and the Output under each query is the real result.
IF DB_ID('SqlLearningDb') IS NULL
CREATE DATABASE SqlLearningDb;
GO
USE SqlLearningDb;
GO
Small table: 7 staff members in 3 departments
DROP TABLE IF EXISTS dbo.Staff;
CREATE TABLE dbo.Staff
(
StaffName VARCHAR(20),
Department VARCHAR(10),
Salary INT
);
INSERT INTO dbo.Staff (StaffName, Department, Salary) VALUES
('Ravi', 'IT', 50000),
('Sita', 'IT', 60000),
('John', 'IT', 40000),
('Meena', 'HR', 30000),
('Arjun', 'HR', 35000),
('Kiran', 'Sales', 45000),
('Pooja', 'Sales', 55000);
GO
SELECT * FROM dbo.Staff;
Messages(7 rows affected)
Output
| StaffName | Department | Salary |
| Ravi | IT | 50000 |
| Sita | IT | 60000 |
| John | IT | 40000 |
| Meena | HR | 30000 |
| Arjun | HR | 35000 |
| Kiran | Sales | 45000 |
| Pooja | Sales | 55000 |
GROUP BY: one row per department (names are gone)
SELECT Department, SUM(Salary) AS DeptTotal
FROM dbo.Staff
GROUP BY Department;
Output
| Department | DeptTotal |
| HR | 65000 |
| IT | 150000 |
| Sales | 100000 |
Window function: every row stays, total is added next to it
SELECT StaffName, Department, Salary,
SUM(Salary) OVER (PARTITION BY Department) AS DeptTotal
FROM dbo.Staff;
Output
| StaffName | Department | Salary | DeptTotal |
| Meena | HR | 30000 | 65000 |
| Arjun | HR | 35000 | 65000 |
| Ravi | IT | 50000 | 150000 |
| Sita | IT | 60000 | 150000 |
| John | IT | 40000 | 150000 |
| Kiran | Sales | 45000 | 100000 |
| Pooja | Sales | 55000 | 100000 |
OVER () with nothing inside = the whole table
SELECT StaffName, Salary,
SUM(Salary) OVER () AS CompanyTotal
FROM dbo.Staff;
Output
| StaffName | Salary | CompanyTotal |
| Ravi | 50000 | 315000 |
| Sita | 60000 | 315000 |
| John | 40000 | 315000 |
| Meena | 30000 | 315000 |
| Arjun | 35000 | 315000 |
| Kiran | 45000 | 315000 |
| Pooja | 55000 | 315000 |
COUNT, AVG, MIN, MAX per department
SELECT StaffName, Department, Salary,
COUNT(*) OVER (PARTITION BY Department) AS PeopleInDept,
AVG(Salary) OVER (PARTITION BY Department) AS DeptAverage,
MIN(Salary) OVER (PARTITION BY Department) AS DeptLowest,
MAX(Salary) OVER (PARTITION BY Department) AS DeptHighest
FROM dbo.Staff;
Output
| StaffName | Department | Salary | PeopleInDept | DeptAverage | DeptLowest | DeptHighest |
| Meena | HR | 30000 | 2 | 32500 | 30000 | 35000 |
| Arjun | HR | 35000 | 2 | 32500 | 30000 | 35000 |
| Ravi | IT | 50000 | 3 | 50000 | 40000 | 60000 |
| Sita | IT | 60000 | 3 | 50000 | 40000 | 60000 |
| John | IT | 40000 | 3 | 50000 | 40000 | 60000 |
| Kiran | Sales | 45000 | 2 | 50000 | 45000 | 55000 |
| Pooja | Sales | 55000 | 2 | 50000 | 45000 | 55000 |
Compare each person with their department average
SELECT StaffName, Department, Salary,
AVG(Salary) OVER (PARTITION BY Department) AS DeptAverage,
Salary - AVG(Salary) OVER (PARTITION BY Department) AS AboveOrBelow
FROM dbo.Staff;
Output
| StaffName | Department | Salary | DeptAverage | AboveOrBelow |
| Meena | HR | 30000 | 32500 | -2500 |
| Arjun | HR | 35000 | 32500 | 2500 |
| Ravi | IT | 50000 | 50000 | 0 |
| Sita | IT | 60000 | 50000 | 10000 |
| John | IT | 40000 | 50000 | -10000 |
| Kiran | Sales | 45000 | 50000 | -5000 |
| Pooja | Sales | 55000 | 50000 | 5000 |
Each person's share of the company salary (%)
SELECT StaffName, Salary,
Salary * 100.0 / SUM(Salary) OVER () AS PercentOfTotal
FROM dbo.Staff;
Output
| StaffName | Salary | PercentOfTotal |
| Ravi | 50000 | 15.873015873015 |
| Sita | 60000 | 19.047619047619 |
| John | 40000 | 12.698412698412 |
| Meena | 30000 | 9.523809523809 |
| Arjun | 35000 | 11.111111111111 |
| Kiran | 45000 | 14.285714285714 |
| Pooja | 55000 | 17.460317460317 |
Practice Questions
- Show each person with the highest salary in the whole company.
- Show each person with the number of people in the company.
- Show each person's share (%) of their DEPARTMENT total.