CTE with Aggregate Functions
Learn how to use aggregate functions such as COUNT, SUM, AVG, MIN and MAX with Common Table Expressions in SQL Server.
What are Aggregate Functions?
Aggregate functions perform calculations on a set of rows and return a single result or a result for each group.
Common aggregate functions in SQL Server include:
- COUNT() - Counts the number of rows.
- SUM() - Calculates the total value.
- AVG() - Calculates the average value.
- MIN() - Returns the minimum value.
- MAX() - Returns the maximum value.
Example Employees Table
EmployeeId | EmployeeName | Department | Salary
--------------------------------------------------
101 | Amit | IT | 60000
102 | Rahul | HR | 45000
103 | Priya | IT | 70000
104 | Sneha | Finance | 55000
105 | Raj | IT | 65000
CTE with AVG()
The following CTE calculates the average salary of employees in each department.
WITH DepartmentSalary AS
(
SELECT
Department,
AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY Department
)
SELECT *
FROM DepartmentSalary;
Result
| Department | Average Salary |
|---|---|
| IT | 65000 |
| HR | 45000 |
| Finance | 55000 |
CTE with COUNT()
We can use COUNT() to find the number of employees in each department.
WITH DepartmentEmployeeCount AS
(
SELECT
Department,
COUNT(*) AS EmployeeCount
FROM Employees
GROUP BY Department
)
SELECT *
FROM DepartmentEmployeeCount;
CTE with SUM()
SUM() can be used to calculate the total salary for each department.
WITH DepartmentTotalSalary AS
(
SELECT
Department,
SUM(Salary) AS TotalSalary
FROM Employees
GROUP BY Department
)
SELECT *
FROM DepartmentTotalSalary;
CTE with MIN() and MAX()
MIN() and MAX() can be used to find the lowest and highest salary in each department.
WITH DepartmentSalaryRange AS
(
SELECT
Department,
MIN(Salary) AS MinimumSalary,
MAX(Salary) AS MaximumSalary
FROM Employees
GROUP BY Department
)
SELECT *
FROM DepartmentSalaryRange;
Multiple Aggregate Functions
Multiple aggregate functions can be used inside the same CTE.
WITH DepartmentStatistics AS
(
SELECT
Department,
COUNT(*) AS EmployeeCount,
SUM(Salary) AS TotalSalary,
AVG(Salary) AS AverageSalary,
MIN(Salary) AS MinimumSalary,
MAX(Salary) AS MaximumSalary
FROM Employees
GROUP BY Department
)
SELECT *
FROM DepartmentStatistics;
Filtering Aggregate Results
Once the aggregate result is generated by the CTE, the main query can filter that result.
WITH DepartmentStatistics AS
(
SELECT
Department,
COUNT(*) AS EmployeeCount,
AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY Department
)
SELECT *
FROM DepartmentStatistics
WHERE AverageSalary > 50000;
CTE with HAVING
The HAVING clause can be used inside the CTE when we want to filter grouped results.
WITH DepartmentStatistics AS
(
SELECT
Department,
AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY Department
HAVING AVG(Salary) > 50000
)
SELECT *
FROM DepartmentStatistics;
How CTE with Aggregate Functions Works
- The CTE starts with the WITH keyword.
- The required rows are selected from the table.
- GROUP BY groups the records when required.
- Aggregate functions perform calculations on the groups.
- The main query reads the result produced by the CTE.
Advantages
- Makes aggregate queries easier to understand.
- Separates aggregation logic from the final query.
- Makes complex grouped queries more readable.
- Allows the aggregate result to be filtered in the main query.
- Helps organize reporting queries.
Important Points
- Aggregate functions can be used inside a CTE.
- GROUP BY is commonly used with aggregate functions.
- HAVING can filter grouped results.
- The main query can further filter or sort the CTE result.
- Multiple aggregate functions can be used in the same CTE.
Summary
CTEs can be combined with aggregate functions to perform calculations such as COUNT, SUM, AVG, MIN and MAX.
Using a CTE keeps the aggregation logic organized and makes complex reporting queries easier to read and maintain.