Group Functions in SQL

What Are Group Functions?

Group functions operate on sets of rows to give one result per groupgroup-function

Types of Group Functions

  • COUNT
  • AVG
  • MAX
  • MIN
  • SUM

Group function syntax

SELECT group_function(column), ...
FROM table
[WHERE condition]
[ORDER BY column];

COUNT

  • It is used to count the no of records in a column
  • COUNT(*) returns the number of rows in a table

Query: Count the number of employee working in department number 80

SELECT COUNT(*) FROM employees WHERE department_id=80;
Result value:34

note:COUNT(expr) returns the number of rows with non-null values

SELECT COUNT(commission_pct) FROM employees WHERE department_id = 80;
Result value: 3

AVG

  • It is used to find the average value of the column.

Query: find the average salary of employees who are working in department number 80

SELECT AVG(salary) FROM employees where department_id=80

MAX

  • It is used to find max value in column

Query: Display the max sal of the employee table?

SELECT MAX(salary) FROM employees;

MIN

  • It is used to find minimum value in the column.

Query: Display the minimum salary of the employee table?

SELECT MIN(salary) FROM employees;

SUM

  • It is used to find the summation of all the values within the column

Query:find the sum of all the salary of employees table

SELECT SUM(salary) FROM employees;

Leave a Reply

Your email address will not be published. Required fields are marked *