SQL Aggregate Functions

christian.dinh's avatar
Published Jul 30, 2021Updated Sep 9, 2021
Contribute to Docs

In SQL, aggregate functions perform a calculation on a set of values and return a single value. They are often used with the GROUP BY clause of the SELECT statement.

Note: Except for COUNT(*), aggregate functions ignore all NULL values.

  • Learn to analyze data with SQL and prepare for technical interviews.
    • Includes 9 Courses
    • With Certificate
    • Beginner Friendly.
      18 hours
  • In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
    • Beginner Friendly.
      5 hours

Example

List all the years and their number of movies, but only the years with more than 5 movies:

SELECT year,
COUNT(*)
FROM movies
GROUP BY year
HAVING COUNT(*) > 5;

Aggregate Functions

AVG()
Returns the average value in a column.
COUNT()
Returns the number of rows that match the specified criteria.
MAX()
Returns the largest value in a column.
MIN()
Returns the smallest value in a column.
SUM()
Returns the sum of all the value in that column.

All contributors

Contribute to Docs

Learn SQL on Codecademy

  • Learn to analyze data with SQL and prepare for technical interviews.
    • Includes 9 Courses
    • With Certificate
    • Beginner Friendly.
      18 hours
  • In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
    • Beginner Friendly.
      5 hours