In corporate business intelligence, decision-makers rarely inspect raw individual transaction records. Instead, they demand aggregate metrics: total annual revenue, average customer spending, minimum delivery times, and total student counts. SQL Aggregate Functions perform a mathematical calculation across multiple row values and return a single summary value.
| Function & Syntax | Calculation Performed | NULL Handling |
|---|---|---|
| COUNT(*) | Returns the total number of rows matching the query. | Includes rows with NULLs. |
| COUNT(column) | Returns total count of non-NULL values in that column. | Ignores NULLs. |
| SUM(numeric_col) | Calculates total sum of all values in the column. | Ignores NULLs. |
| AVG(numeric_col) | Calculates arithmetic mean ((\sum / N)) of non-NULL values. | Ignores NULLs. |
| MIN(column) | Finds minimum value (works on numbers, dates, strings). | Ignores NULLs. |
| MAX(column) | Finds maximum value (works on numbers, dates, strings). | Ignores NULLs. |
-- Calculate high-level student performance metrics
SELECT
COUNT(*) AS total_students_enrolled,
COUNT(phone_number) AS students_with_phone,
COUNT(DISTINCT course) AS unique_courses_offered,
ROUND(AVG(gpa), 2) AS university_average_gpa,
MIN(gpa) AS lowest_gpa,
MAX(gpa) AS highest_gpa
FROM students;
| total_students | students_with_phone | unique_courses | avg_gpa | lowest_gpa | highest_gpa |
|---|---|---|---|---|---|
| 4 | 3 | 3 | 3.80 | 3.68 | 3.92 |
Next, let us learn how to compute these metrics for specific categories using the SQL GROUP BY Clause!