Aggregation Functions in SQL
What are Aggregation Functions?
Aggregation functions in SQL are built-in functions that perform a calculation on a set of values and return a single value. These are commonly used with GROUP BY
to summarize large datasets, such as totals, averages, maximum values, etc.
Common Aggregate Functions:
1. COUNT()
Purpose: Counts the number of rows.
Â
Syntax:
Â
Example: Count how many students are in the table.
2. SUM()
Purpose: Adds up all values in a numeric column.
Â
Syntax:
Â
Example: Total marks of all students.
3. AVG()
Purpose: Calculates the average of a numeric column.
Â
Syntax:
Â
Example: Find the average marks in a subject.
4. MIN()
Purpose: Finds the minimum (smallest) value in a column.
Â
Syntax:
Â
Example: Youngest student’s age.
5. MAX()
Purpose: Finds the maximum (largest) value in a column.
Â
Syntax:
Â
Example: Highest salary among teachers.
Using Aggregate Functions with GROUP BY
You can combine aggregate functions with the GROUP BY
clause to compute summaries per group (like per department, class, etc.).
Â
Example:
Â
This will show the average salary in each department.