SQL Aggregate Functions With Examples

SQL Aggregate Functions basically operate on multiple columns to perform the operations and serve to represent the output as an entity representing the operation executed.

SQL-Aggregate_functions

SQL Aggregate Functions

Syntax:

  • DISTINCT enables the user to select distinct values from the table i.e. if multiple attributes contain the same value, then only single distinct value is considered for the calculations.
  • ALL makes sure that even the repeated values are considered for the calculations.

Aggregate Functions in SQL:

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

1. SQL AVG() function

SQL AVG() function returns the average of all the selected values from the corresponding column of the table.

Let’s us consider the following table to understand the Aggregate functions:

Table Name: Details

ID Name Amount Age
1 Safa 5000 21
2 Aman 2500 23
3 Rehaan 20000 25
4 Seema 12000 25

Example:

Output:


2. SQL MIN() function

SQL MIN() function returns the minimum value of all the selected values from the corresponding column of the table.

Example:

Output:


3. SQL MAX() function

SQL MAX() function returns the maximum value of all the selected values from the corresponding column of the table.

Output:


4. SQL SUM() function

SQL SUM() function returns the summation of all the selected values from the corresponding column of the table.

Example 1: Basic Understanding of SUM() Function

Output:

Example 2: SQL SUM() with GROUP BY Clause

SQL SUM() can be framed together with SQL GROUP BY Clause to represent the output results by a particular label/values.

Output:

SUM-with-GROUP-BY

SUM() with GROUP BY

Example 3: SQL SUM() with HAVING Clause

SQL SUM() function can be used along with SQL HAVING Clause; HAVING Clause is basically used to specify the condition to be operated on the set of values in the table.

Output:

SUM() with HAVING Clause

5. SQL COUNT() function

Example 1: COUNT() function to return the count of a particular column of a table

Output:

Example 2: COUNT(*) function

This function returns the count of all the values present in the set of records of a table.

Output:

Example 3: COUNT() with WHERE Clause

Output:

Example 4: COUNT() with GROUP BY Clause

Output:

COUNT() with GROUP BY Clause

Example 5: COUNT() with HAVING Clause

Output:

COUNT() with HAVING Clause

Conclusion

Thus, in this article, we have understood different SQL Aggregate Functions.


Reference

By admin

Leave a Reply

%d bloggers like this: