Aggregate Functions in SQL perform some calculations on more than one value to return a single value. SQL has many aggregate functions, including average, count, sum, min, and max. It can operate on multiple rows or multiple pieces of data at once to tell us the total, minimum, maximum and count .All aggregate functions ignore NULL values while calculating, except the Count function.
Introduction:
An Aggregate Functions in SQL returns one value after calculating multiple columns value. We often use aggregate functions with the SELECT statement’s GROUPBY and HAVING clauses.
Five types of SQL AGGREGATE functions:
Count()
sum()
avg()
min()
max()
Column References:
In SQL, aggregate functions are crucial in calculating a set of values and returning a single value. These Functions are beneficial when dealing with large datasets. When using aggregate functions in SQL, it is essential to understand column references. A column reference is a name containing the data you want to aggregate. To use an aggregate function with a column reference, specify the column’s name in the function’s parentheses.
SELECT AVG(stock_quantity)
From books;
Aggregate functions in SQL are important to anyone working with large amount of data and trying to seek valuable insight.
COUNT() Function
The count() function returns the number of rows in a database table.
Example:
We will demonstrate the ‘Books table’ from the sample database.
![](https://static.wixstatic.com/media/aaebc2_b69b69a7551e44c8b46f7134c33cb30f~mv2.png/v1/fill/w_980,h_475,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/aaebc2_b69b69a7551e44c8b46f7134c33cb30f~mv2.png)
The following SQL statement fetches the number of rows in the table.
SELECTÂ COUNT(*) FROMÂ books;
This will produce the following result.
![](https://static.wixstatic.com/media/aaebc2_843e4c67a215403aa5a773de6def9f53~mv2.png/v1/fill/w_166,h_67,al_c,q_85,enc_auto/aaebc2_843e4c67a215403aa5a773de6def9f53~mv2.png)
The following SQL statement fetches the number author first_name in the table.
SELECTÂ COUNT(author_fname) FROMÂ books;
This will produce the following result.
![](https://static.wixstatic.com/media/aaebc2_23513dff67b84bad8bd847ee43038db1~mv2.png/v1/fill/w_238,h_66,al_c,q_85,enc_auto/aaebc2_23513dff67b84bad8bd847ee43038db1~mv2.png)
The below-given command will display distinct(unique) count of author first_name.
SELECTÂ COUNT(DISTINCTÂ author_fname) FROMÂ books;
This will produce the following result.
![](https://static.wixstatic.com/media/aaebc2_458e0cee6db6450bad7fb0ee1b3b666e~mv2.png/v1/fill/w_289,h_79,al_c,q_85,enc_auto/aaebc2_458e0cee6db6450bad7fb0ee1b3b666e~mv2.png)
one more example,
SELECTÂ COUNT(*) FROMÂ books WHEREÂ title LIKEÂ '%the%';
This will produce the following result.
![](https://static.wixstatic.com/media/aaebc2_99123eb7bf7042d08202ae9a3f25b9d8~mv2.png/v1/fill/w_157,h_49,al_c,q_85,enc_auto/aaebc2_99123eb7bf7042d08202ae9a3f25b9d8~mv2.png)
GROUP BYÂ summarizes or aggregates identical data into single rows.
Let’s look at how we can use GROUP BY and ORDER BY functions with the COUNT function.
SELECT author_lname,COUNT(*) AS books_written
FROM books
GROUP BY author_lname
ORDER BY books_written DESC;
This will produce the following result.
![](https://static.wixstatic.com/media/aaebc2_2c6dea7414fb4e38aee4b9be114f9015~mv2.png/v1/fill/w_340,h_310,al_c,q_85,enc_auto/aaebc2_2c6dea7414fb4e38aee4b9be114f9015~mv2.png)
2. SUM() Function
The SUM() function returns the total sum of column.
The following SQL statement is to sum all the pages each author has written.
SELECT author_lname, COUNT(*), SUM(pages)
FROM books
GROUP BY author_lname;
This will produce the following result.
![](https://static.wixstatic.com/media/aaebc2_58521391abe24121b542be2b238fa97d~mv2.png/v1/fill/w_400,h_321,al_c,q_85,enc_auto/aaebc2_58521391abe24121b542be2b238fa97d~mv2.png)
3. AVG() Function
The AVG() function calculates the average of set of values.
The following SQL command calculates the average stock quantity for books released in the same year.
SELECT
released_year,
AVG(stock_quantity),
COUNT(*) FROM books
GROUP BY released_year;
This will produce the following result.
![](https://static.wixstatic.com/media/aaebc2_f2ac909d1c8c41acbcdc13bd271bd7f4~mv2.png/v1/fill/w_475,h_438,al_c,q_85,enc_auto/aaebc2_f2ac909d1c8c41acbcdc13bd271bd7f4~mv2.png)
4.MIN() Function
The MIN() aggregate function returns the lowest value(minimum) in a set of non-NULL values.
The following SQL command find the year of each author published their first book
SELECTÂ author_lname, MIN(released_year) FROMÂ books GROUPÂ BYÂ author_lname;
This will produce the following result.
![](https://static.wixstatic.com/media/aaebc2_bbff816ef2254f418a19d23328393587~mv2.png/v1/fill/w_385,h_316,al_c,q_85,enc_auto/aaebc2_bbff816ef2254f418a19d23328393587~mv2.png)
5. Max() Function
The MAX() aggregate function returns the highest value(maximum) in a set of non-NULL values.
The following SQL command find the year of each author published their last book
SELECTÂ author_lname, MAX(released_year) FROMÂ books GROUPÂ BYÂ author_lname;
This will produce the following result.
![](https://static.wixstatic.com/media/aaebc2_408fb30c8b514112a2de54d49336c21b~mv2.png/v1/fill/w_367,h_325,al_c,q_85,enc_auto/aaebc2_408fb30c8b514112a2de54d49336c21b~mv2.png)
6.Subqueries:
It is a query within a larger query. It is located within the WHERE clause of their enclosing statement. Their function is to set the search conditions for the WHERE clause. Each kind of subquery produces a different result.
Some subqueries produce a list of values that is then used as input by the enclosing statement. Other subqueries produce a single value that the enclosing statement then evaluates with a comparison operator. A third kind of subquery returns a values of true or false.
Example:
The following SQL command ,first it executes the sub-query (find max number of pages from books table )and then select title and pages from the output of subquery.
SELECT title, pages FROM books
WHERE pages = (SELECT MAX(pages) FROM books);
This will produce the following result.
![](https://static.wixstatic.com/media/aaebc2_6422b73f6ef644d9a480c94f437f1f18~mv2.png/v1/fill/w_451,h_87,al_c,q_85,enc_auto/aaebc2_6422b73f6ef644d9a480c94f437f1f18~mv2.png)
Grouping By Multiple Columns:
You can GROUP BY two columns in SQL. TO group by multiple categories, just separate column names with commas. User can apply aggregate functions like sum, count, avg, min and max to each group, make it easier to perform data analysis.
Example:
SELECT CONCAT(author_fname, ' ', author_lname) AS author, COUNT(*)
FROM books
GROUP BY author;
This will produce the following result.
![](https://static.wixstatic.com/media/aaebc2_d65ba9cfaebc4a04b3879c9da97db52e~mv2.png/v1/fill/w_328,h_346,al_c,q_85,enc_auto/aaebc2_d65ba9cfaebc4a04b3879c9da97db52e~mv2.png)
MIN And Max With GROUPBY
GROUPBY clause along with sql Max() can be used to find the maximum value of a column over each group and MIN() can be used to fine the minimum value of a column over each group.
Example:
SELECT author_lname, author_fname,
COUNT(*) as books_written,
MAX(released_year) AS latest_release,
MIN(released_year) AS earliest_release
FROM books
GROUP BY author_lname, author_fname;
This will produce the following result.
![](https://static.wixstatic.com/media/aaebc2_3f9175b308394c389a629f531fcd9f85~mv2.png/v1/fill/w_700,h_354,al_c,q_85,enc_auto/aaebc2_3f9175b308394c389a629f531fcd9f85~mv2.png)
GROUPBY with HAVING clause:
The HAVING clause is a filter that acts like a WHERE clause, but on groups of rows rather than on individual rows.
Example:
The following SQL command ,Group by author followed by Having clause sum(pages) >=200
SELECT CONCAT(author_fname, ' ', author_lname) AS author,
SUM(pages) AS Total_pages
FROM BOOKS
GROUP BY author
HAVING Total_pages>=200;
This will produce the following result.
![](https://static.wixstatic.com/media/aaebc2_abb19ead309d4a71b2b9fe077dcb1519~mv2.png/v1/fill/w_361,h_343,al_c,q_85,enc_auto/aaebc2_abb19ead309d4a71b2b9fe077dcb1519~mv2.png)
CONCLUSION:
Aggregate Functions are easily to calculate large datasets quickly and provide insights from complex data by calculating single values like sum ,avg, min, max. As per our title it simplifies complex queries and improve query performance. I hope you will be clear about aggregate functions in SQL.
Thanks for reading!