SQL Aggregate Functions
In this article let us explore a new topic in SQL called aggregate functions. First let us understand what is aggregate function and how does it actually work.
What is aggregate function?
Aggregate functions in SQL are unique functions that work on a group of rows in a table and produce a single value as a result. These operations are used to calculate a set of numbers and produce summary statistics like sum, count, average, maximum, and minimum. SQL queries frequently employ aggregate procedures to condense data from one or more tables.
After grouping and aggregating, aggregate functions can also be used with the HAVING or WHERE clause to further filter the data. A condition involving an aggregate function can be used to filter the results of a query using the HAVING or WHERE clause.
Below are a few frequently used aggregate functions in SQL. Let us understand each of these functions with the help of various examples.
This function returns the number of records(rows) in a table. The Syntax of the count() function is mentioned below.
Here column_name is the name of the column that you want to know about the number of values and the WHERE clause is used only when specific data which satisfies a condition is extracted.
Ex: consider below "Student" table for understanding the functioning of the Count() function.
Now, let us attach the WHERE clause to the COUNT() and see how it works.
select COUNT(*) FROM Student WHERE Department = 'CSE';
This function returns the sum of all values of a column in a table. Here is the syntax for the sum() function.
Here column_name is the name of the column for which you wanted to find the sum, of values and the WHERE clause is used only when you wanted to apply the SUM() function to the data which satisfies the condition.
Ex: observe the below example for understanding the functioning of the SUM function.
Now let us calculate the sum of the prices of mobiles which are present in the sales table.
This function will return the average of all values present in a column. The syntax of the AVG() function is given below.
Here column_name is the name of the column for which you need to find the average of the values present in the column and the WHERE clause is used only when you wanted to apply AVG() function to the data which satisfies the condition.
Let us consider again the "sales" table for applying the AVG() on it and let us see how it works.
Now let us find out the average of all prices of the products using the AVG() function.
Now let us find the average price of only mobiles by using the WHERE clause in combination with AVG() function.
This function produces the lowest value in a column for a group of rows that satisfy a given criterion. The Syntax of the MIN() function is as follows
Let us consider the below employees table to illustrate the functioning of the MIN function.
Now let us apply the MIN function to find out the minimum value in the salary column.
Now let us find us find out the minimum salary from the R&D department.
The MAX function in SQL is used to return the highest value in a column for a group of rows that satisfy a given condition in a table. The MAX syntax is as follows:
Consider again the employees table to understand the functioning of MAX function.
Now let us find us find out the maximum value in the salary column using MAX() function.
Now find out the highest salary of the marketing department by using the WHERE clause in combination with MAX() function.
This is all about Aggregate functions in SQL. You may summarise and analyze data from a table or set of tables using aggregate functions. They are necessary for producing summary statistics and can assist you in finding simple answers to your complex data questions.