SQL Aggregate Functions Get Certified
Learn how to calculate totals, averages, minimums, maximums and row counts using SQLite-compatible SQL aggregate functions.
What are Aggregate Functions?
Aggregate functions perform a calculation on multiple rows and return a single result.
They are used to summarize and analyze data. The most common ones are COUNT(), SUM(),
AVG(), MIN() and MAX().
Common Aggregate Functions
COUNT()COUNT(*)SUM()SUM(salary)AVG()AVG(salary)MIN()MIN(salary)MAX()MAX(salary)COUNT() Function
The COUNT() function returns the number of rows or non-NULL values that match the specified expression.
What this code does: Counts every row in the employees table with COUNT(*) and labels the result total_employees. The table has ten employees, so the query returns a single row containing 10.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT COUNT(*) AS total_employees
FROM employees;
COUNT() with a Column
When a column is given to COUNT(), NULL values in that column are not counted.
What this code does: This table has an extra row, Liam Clark, whose salary is NULL. COUNT(*) counts all 11 rows, while COUNT(salary) skips the NULL and returns 10. The two columns of the result show the difference side by side.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000),
('Liam Clark', 'Sales', NULL)
)
SELECT COUNT(*) AS all_rows,
COUNT(salary) AS employees_with_salary
FROM employees;
SUM() Function
The SUM() function calculates the total of the numeric values in a column.
What this code does: Adds up the salaries of all ten employees and returns one row named total_salary with the value 737000.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT SUM(salary) AS total_salary
FROM employees;
AVG() Function
The AVG() function calculates the average value of a numeric column. NULL values are ignored.
What this code does: Divides the total salary (737000) by the number of salaries (10) and returns 73700 as average_salary.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT AVG(salary) AS average_salary
FROM employees;
MIN() Function
The MIN() function returns the smallest value found in a column.
What this code does: Looks through all salaries and returns the smallest one, 54000, which belongs to Sophia Wilson. Note that MIN() returns only the value, not the employee’s name.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT MIN(salary) AS lowest_salary
FROM employees;
MAX() Function
The MAX() function returns the largest value found in a column.
What this code does: Returns the largest salary, 102000, which belongs to Matthew Anderson. Like MIN(), it returns only the value.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT MAX(salary) AS highest_salary
FROM employees;
Using Multiple Aggregate Functions
Several aggregate functions can be used in one query to produce many summary values at once.
What this code does: Calculates five summaries in a single pass over the table and returns one row: 10 employees, a total of 737000, an average of 73700, a lowest salary of 54000 and a highest salary of 102000.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT
COUNT(*) AS total_employees,
SUM(salary) AS total_salary,
AVG(salary) AS average_salary,
MIN(salary) AS lowest_salary,
MAX(salary) AS highest_salary
FROM employees;
Aggregate Functions with WHERE
Combine aggregate functions with WHERE to calculate values only for rows that meet a condition.
What this code does: First keeps only employees earning more than 60000 (seven of the ten), and then averages their salaries. The total of those seven is 566000, so the result is about 80857.14. WHERE runs before the aggregate is calculated.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT AVG(salary) AS average_salary
FROM employees
WHERE salary > 60000;
COUNT(DISTINCT)
COUNT(DISTINCT column_name) counts only the unique non-NULL values in a column.
What this code does: Several employees share a department, but each department is counted once. The result is 5: Engineering, Marketing, Sales, Finance and HR.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT COUNT(DISTINCT department) AS total_departments
FROM employees;
Aggregate Functions with GROUP BY
Aggregate functions become especially useful with GROUP BY, which calculates a separate result for each group.
What this code does: Splits the employees into one group per department and averages the salaries in each group: Engineering 95000, Marketing 60500, Sales 62000, Finance 76500 and HR 54000.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT department, AVG(salary) AS average_salary
FROM employees
GROUP BY department;
SUM() with GROUP BY
Use SUM() with GROUP BY to total a column for each group.
What this code does: Adds up the salaries per department: Engineering 285000, Marketing 121000, Sales 124000, Finance 153000 and HR 54000. These five totals add up to 737000, the same as the overall SUM().
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department;
COUNT() with GROUP BY
COUNT() with GROUP BY counts the rows in each group.
What this code does: Counts how many employees work in each department: Engineering 3, Marketing 2, Sales 2, Finance 2 and HR 1.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
Aggregate Functions with HAVING
HAVING filters grouped results after the aggregate has been calculated. WHERE cannot do this because it runs before grouping.
What this code does: Groups employees by department, calculates each average, and keeps only groups whose average is above 60000. HR (54000) is removed, leaving Engineering (95000), Marketing (60500), Sales (62000) and Finance (76500).
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT department, AVG(salary) AS average_salary
FROM employees
GROUP BY department
HAVING AVG(salary) > 60000;
Rounding Aggregate Results
Averages often produce long decimals. Wrap the aggregate in ROUND() to control the number of decimal places.
What this code does: Calculates the average salary of employees earning more than 60000 (80857.142857…) and rounds it to two decimal places, giving 80857.14.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT ROUND(AVG(salary), 2) AS average_salary
FROM employees
WHERE salary > 60000;
Advantages of Aggregate Functions
- Summarize large amounts of data quickly.
- Calculate totals, averages, minimums and maximums.
- Count rows and unique values.
- Work effectively with
GROUP BY. - Can be combined with
WHEREandHAVINGfor deeper analysis.
Best Practices
- Use meaningful aliases for calculated results.
- Use
COUNT(*)when you need to count rows. - Remember that most aggregate functions ignore NULL values.
- Use
WHEREto filter rows andHAVINGto filter groups. - Use
GROUP BYwhen you need a result for each group.
Aggregate functions turn many rows into useful summary information. Click Test My Code on any example above to run it live, or Code Explanation to walk through the query block by block.
🧪 Test Your SQL Code
Edit the SQL code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.