What are Aggregate Functions in SQL? Easy Guide for Beginners

Aggregate = “Total / Summary”.

In SQL, aggregate functions are used to calculate values from multiple rows together (instead of checking each row one by one).

Example: In a class of students, instead of checking every student’s marks separately, we calculate total marks, average marks, highest marks, etc.

Aggregate Functions

Common Aggregate Functions:

  1. SUM() → Adds values
    • Example: SUM(salary) = total salary of all employees.
    • Like: Total bill amount in a restaurant .
  2. AVG() → Average value
    • Example: AVG(marks) = average marks of students.
    • Like: Average run rate in cricket .
  3. COUNT() → Number of rows
    • Example: COUNT(student_id) = total students in class.
    • Like: Counting how many players are on the field.
  4. MAX() → Highest value
    • Example: MAX(salary) = highest salary in company.
    • Like: Highest score in cricket match.
  5. MIN() → Lowest value
    • Example: MIN(marks) = lowest marks in exam.
    • Like: Minimum bus ticket price.

Real-Life Analogy (Cricket Example):

Imagine we have a scoreboard of players’ runs:

PlayerRuns
Virat80
Rohit50
Dhoni40
Hardik30
  • SUM(runs) → 200 (total team score)
  • AVG(runs) → 50 (average runs)
  • COUNT(runs) → 4 (number of players)
  • MAX(runs) → 80 (highest score)
  • MIN(runs) → 30 (lowest score)

Quick Recap for Freshers:

  • Aggregate = Summary function.
  • Works on group of rows, not single row.
  • Used with GROUP BY often (like marks per subject, sales per city).