SQL aggregate functions retrieve information about the data in a database. You can use COUNT to find out how many records are in a database, for instance. Accepted aggregate functions are: COUNT, MIN, MAX, AVG, and SUM.
There are occasions where you may want to get information about the data, but not the data itself. Perhaps you want to know how many employees are based in each office, or who has worked for your business the longest. This is where SQL aggregate functions come in.
In this guide, we’ll discuss the basics of SQL aggregate functions, how they work, and how you can use them in your queries.
Note: We are using PostgreSQL for the purposes of this article.
SQL Aggregate Functions
SQL aggregate functions collate information about what is in a database. For instance, you can use SUM to find the total of all the values in a column. Aggregate functions save you time when you need to find out information that involves aggregating records.,
Here is a list of the aggregate functions in SQL you can use:
Let’s walk through each of these individually. We’re going to be referring to a database of employees throughout this tutorial.
Aggregate functions are used at the beginning of the SQL SELECT statement.
If you query both a column and an aggregate function in the same SELECT statement, you must use an SQL GROUP BY clause. This clause tells SQL how to represent the data in your query.
The SQL COUNT function returns the total number of rows returned by a query. Using a WHERE statement, the COUNT function returns the number of rows that meet your condition.
For example, say you wanted to know how many employees work in the Stamford branch of your business. You could find this information out using the following query:
SELECT COUNT(name) FROM employees WHERE branch = "Stamford";
Our query returns the number of employees working at the Stamford branch:
SQL MIN and MAX
The SQL MIN function returns the smallest value within a column. An SQL MAX statement returns the largest value in a column. Both of these statements are SQL aggregate functions.
For example, say you want to get the lowest number of employee of the month awards held by a single person. We could retrieve this data using this query:
SELECT MIN(employee_month_awards) FROM employees;
Our query returns:
Say we wanted to get the highest number of employee of the month awards held by a single person. To do so, we would use the MAX function instead:
SELECT MAX(employee_month_awards) FROM employees;
The output for our query is as follows:
Of note, you can use the MIN and MAX functions on both numeric and alphabetic information in your database.
If you wanted to get the name that appeared first in the alphabet from your database, you could use MIN(name). Likewise, MAX(name) could be used to get the name that comes last in the alphabet.
The SQL AVG function returns the average value of a particular column.
Let’s say we want to get the average number of employee of the month awards held by each employee. We would use the following query to accomplish this goal:
SELECT AVG(employee_month_awards) FROM employees;
Our query calculates the average of the employee of the month data and returns the following result set:
The SQL SUM function finds the total sum of a particular column.
Suppose you want to find out how many employee of the month awards have ever been issued. This information is readily accessible if you use an SQL SUM statement:
SELECT SUM(employee_month_awards) FROM employees;
Our query returns the following:
SQL aggregate functions return information about the data in a database. AVG, for instance, returns the average of the values in a database column. There are five aggregate functions, which are: MIN, MAX, COUNT, SUM, and AVG.
Are you up for a challenge?
Write an aggregate function that finds out the number of employees with the name “Linda”. Compare your query with our queries above to see if it makes sense.
We have written a comprehensive guide on how to learn SQL. If you’re looking for new learning resources, check out the guide on our How to Learn SQL page.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more