Skip to main content

Aggregate functions in SQL

SQL Aggregate Function.
                        In relational database management the multiple rows are group to get single aggregated value by using Aggregate function. The following are Aggregate function in SQL.
  • AVG - Calculate Average from set of rows.
  • SUM- Find the total from set of rows.
  • COUNT- Find the number of rows.
  • MIN- Find minimum value from set of rows.
  • MAX-Find maximum value from set of rows.
AVG in SQL:
 id     Emp_name      Age         Salary
 1 Jebas  35 40,000
 2             Thomas  30 80,000
 3 Mark              25 40,000
 4         Harry  42 50,000
 5 Willam  45 100,000
 6 James  32 35000
 7 Mock  22 15000
SELECT  AVG(salary)As AverageSalary   FROM Employee;
In this AverageSalary is column alias. Now the output
AverageSalary
 51428.571428

SUM in SQL
SELECT 
      SUM(salary)As TotalSalary
  FROM Employee;
TotalSalary
 360000

COUNT in SQL
SELECT 
      COUNT(Emp_name)As TotalEmployeeInCompany
  FROM Employee;
TotalEmployeeInCompany
     7

MIN in SQL
SELECT 
      MIN(Salary)As MinimumSalary
  FROM Employee;
MinimumSalary
 15000

MAX in SQL
SELECT 
      MAX(Salary)As MaximumSalary
  FROM Employee;
MaximumSalary
100000

To get employee name who got maximum salary, use sub-query.
SELECT 
      Emp_name 
  FROM Employee where salary=(SELECT 
      Max(salary)
  FROM Employee);
Emp_name
Willam

To get employee name who got minimum salary, use sub-query.
SELECT 
      Emp_name 
  FROM Employee where salary=(SELECT 
      MIN(salary)
  FROM Employee);
Emp_name
Mock

Thank You. Like Share and Subscribe 👍 folowlect.com

Comments