Lesson 13 Intermediate Level
13 of 30 lessons

Aggregate Functions

Learn about SUM, AVG, COUNT, MAX, MIN functions for data analysis. Aggregate functions are essential for summarizing and analyzing data in SQL.

What are Aggregate Functions?

Aggregate functions perform a calculation on a set of values and return a single value. These functions are often used with GROUP BY to summarize data.

Common Aggregate Functions

Function Description Example
COUNT() Counts the number of rows SELECT COUNT(*) FROM employees;
SUM() Returns the total sum of a numeric column SELECT SUM(salary) FROM employees;
AVG() Returns the average value SELECT AVG(salary) FROM employees;
MAX() Returns the highest value SELECT MAX(salary) FROM employees;
MIN() Returns the lowest value SELECT MIN(salary) FROM employees;

Example Usage

-- Get the total number of employees
SELECT COUNT(*) FROM employees;

-- Get average salary
SELECT AVG(salary) FROM employees;

-- Find highest and lowest salary
SELECT MAX(salary), MIN(salary) FROM employees;

-- Total salary per department
SELECT department_id, SUM(salary)
FROM employees
GROUP BY department_id;

Practice Task