SQL Aggregate Functions – COUNT, SUM, AVG, MIN, MAX Tutorial

SQL Aggregate Functions – COUNT, SUM, AVG, MIN, MAX

Aggregate Functions in SQL are used to perform calculations on multiple rows of a table and return a single value. They are often used with GROUP BY or HAVING clauses to summarize data.


1. COUNT()

Definition: Returns the number of rows that match a specified condition.

SELECT COUNT(*) AS total_students
FROM Student;

Counts all students in the Student table.


2. SUM()

Definition: Returns the total sum of a numeric column.

SELECT SUM(salary) AS total_salary
FROM Employee;

Calculates the total salary of all employees.


3. AVG()

Definition: Returns the average value of a numeric column.

SELECT AVG(age) AS average_age
FROM Student;

Finds the average age of students.


4. MIN()

Definition: Returns the smallest value in a column.

SELECT MIN(salary) AS lowest_salary
FROM Employee;

Finds the lowest salary among employees.


5. MAX()

Definition: Returns the largest value in a column.

SELECT MAX(salary) AS highest_salary
FROM Employee;

Finds the highest salary among employees.


Lab Practice Tasks

  1. Count the total number of students in the Student table.
  2. Calculate the total and average salary of employees.
  3. Find the minimum and maximum ages of students.
  4. Use GROUP BY with COUNT to find the number of students in each department.

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন