Learn SQL: Aggregate Functions

Learn powerful functions for performing complex database operations with ease.

Start[missing "en.views.course_landing_page.learn-sql.course_illustration" translation]

Key Concepts

Review core concepts you need to learn to master this subject

SQL aggregate functions

SQL aggregate functions perform a calculation on a set of values and return a single value:

  • COUNT()
  • SUM()
  • MAX()
  • MIN()
  • AVG()

SQL COUNT() aggregate function

The COUNT() aggregate function in SQL returns the total number of rows that match a specified criteria. For instance, to find the total number of employees who have more than 5 years of experience, the above query can be used. A column name of the table can also be used instead of *. Unlike COUNT(*), This variation COUNT(column_name) will not count NULL values for that column.

Usage of SQL SUM() aggregate function

The SUM() aggregate function in SQL returns the sum value of a column. For instance, to find the total disbursed salary, the above query can be used.

Finding average value of a column in SQL

The AVG() aggregate function in SQL returns the average value in a column. For instance, to find the average amount of salary for the employees having experience less than 5 years, the above query can be used.

Average value of a column in SQL

The AVG() aggregate function in SQL returns the average value in a column. For instance, to find the average amount of salary for the employees having experience less than 5 years, the above query can be used.

ROUND() function in SQL

The SQL ROUND() function will round a number value to a specified number of places. It takes two arguments: a number, and a number of decimal places. It can be combined with other aggregate functions, as shown in the query above. This query will calculate the average rating of movies from 2015, rounding to 2 decimal places.

GROUP BY clause in SQL

The SQL GROUP BY clause will group records in a result set by identical values in one or more columns. It is often used in combination with aggregate functions to query information of like records. The GROUP BY clause can come after FROM or WHERE but must come before any ORDER BY or LIMIT clause. The query above will count the number of movies per rating.

Grouping and Ordering selected columns by number

The SQL GROUP BY and ORDER BY clauses can reference the selected columns by number in which they appear in the SELECT statement. The query above will count the number of movies per rating, and will GROUP BY column 2 (rating) and ORDER BY column 1 (total_movies) in the SELECT statement.

HAVING clause in SQL

The SQL HAVING clause is used to further filter result set groups provided by the GROUP BY clause. HAVING is often used with aggregate functions to filter the result group based on an aggregate property. The query above will filter only years where more than 5 movies were released per year.

Finding largest value of a column in SQL

The MAX() aggregate function in SQL returns the largest value in a column. For instance, to find the largest value of Amt column from the table named tbl_Transactions;, SELECT MAX(Amt) from tbl_Transactions; query can be used.

Largest value of a column in SQL

The MAX() aggregate function in SQL returns the largest value in a column. For instance, to find the largest value of the amount column from the transactions table, the above query can be used.

Finding smallest value of a column in SQL

The MIN() aggregate function in SQL returns the smallest value in a column. For instance, to find the smallest value of Amt column from the table named tbl_Transactions, SELECT MIN(Amt) from tbl_Transactions; query can be used.

Smallest value of a column in SQL

The MIN() aggregate function in SQL returns the smallest value in a column. For instance, to find the smallest value of the amount column from the table named transactions, the above query can be used.

Aggregate Functions
Lesson 1 of 1
  1. 1

    We've learned how to write queries to retrieve information from the database. Now, we are going to learn how to perform calculations using SQL. Calculations performed on multiple rows of a table a...

  2. 2

    The fastest way to calculate how many rows are in a table is to use the [...] function. [...] is a function that takes the name of a column as an argument and counts the number of non-empty va...

  3. 3

    SQL makes it easy to add all values in a particular column using [...] . [...] is a function that takes the name of a column as an argument and returns the sum of all the values in that column....

  4. 4

    The [...] and [...] functions return the highest and lowest values in a column, respectively. How many downloads does the most popular app have? [...] The most popular app has 31,090 downl...

  5. 5

    SQL uses the [...] function to quickly calculate the average value of a particular column. The statement below returns the average number of downloads for an app in our database: [...] The ...

  6. 6

    By default, SQL tries to be as precise as possible without rounding. We can make the result table easier to read using the [...] function. [...] function takes two arguments inside the parenth...

  7. 7

    Oftentimes, we will want to calculate an aggregate for data with certain characteristics. For instance, we might want to know the mean IMDb ratings for all movies each year. We could calculate ea...

  8. 8

    Sometimes, we want to [...] a calculation done on a column. For instance, we might want to know how many movies have IMDb ratings that round to 1, 2, 3, 4, 5. We could do this using the followi...

  9. 9

    In addition to being able to group data using [...] , SQL also allows you to filter which groups to include and which to exclude. For instance, imagine that we want to see how many movies of dif...

  10. 10

    Congratulations! You just learned how to use aggregate functions to perform calculations on your data. What can we generalize so far? - [...] : count the number of rows - [...] : the sum of the...

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo

Learn SQL: Aggregate Functions

Start[missing "en.views.course_landing_page.learn-sql.course_illustration" translation]