Skip to content

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
StaffNameDepartmentSalary
RaviIT50000
SitaIT60000
JohnIT40000
MeenaHR30000
ArjunHR35000
KiranSales45000
PoojaSales55000

GROUP BY: one row per department (names are gone)

SELECT Department, SUM(Salary) AS DeptTotal
FROM dbo.Staff
GROUP BY Department;
Output
DepartmentDeptTotal
HR65000
IT150000
Sales100000

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
StaffNameDepartmentSalaryDeptTotal
MeenaHR3000065000
ArjunHR3500065000
RaviIT50000150000
SitaIT60000150000
JohnIT40000150000
KiranSales45000100000
PoojaSales55000100000

OVER () with nothing inside = the whole table

SELECT StaffName, Salary,
       SUM(Salary) OVER () AS CompanyTotal
FROM dbo.Staff;
Output
StaffNameSalaryCompanyTotal
Ravi50000315000
Sita60000315000
John40000315000
Meena30000315000
Arjun35000315000
Kiran45000315000
Pooja55000315000

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
StaffNameDepartmentSalaryPeopleInDeptDeptAverageDeptLowestDeptHighest
MeenaHR300002325003000035000
ArjunHR350002325003000035000
RaviIT500003500004000060000
SitaIT600003500004000060000
JohnIT400003500004000060000
KiranSales450002500004500055000
PoojaSales550002500004500055000

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
StaffNameDepartmentSalaryDeptAverageAboveOrBelow
MeenaHR3000032500-2500
ArjunHR35000325002500
RaviIT50000500000
SitaIT600005000010000
JohnIT4000050000-10000
KiranSales4500050000-5000
PoojaSales55000500005000

Each person's share of the company salary (%)

SELECT StaffName, Salary,
       Salary * 100.0 / SUM(Salary) OVER () AS PercentOfTotal
FROM dbo.Staff;
Output
StaffNameSalaryPercentOfTotal
Ravi5000015.873015873015
Sita6000019.047619047619
John4000012.698412698412
Meena300009.523809523809
Arjun3500011.111111111111
Kiran4500014.285714285714
Pooja5500017.460317460317

Practice Questions

  1. Show each person with the highest salary in the whole company.
  2. Show each person with the number of people in the company.
  3. Show each person's share (%) of their DEPARTMENT total.