0% found this document useful (0 votes)
11 views

grouping and group-by clause

The document explains group functions in SQL, detailing how they operate on rows to return single values per group. It covers key functions such as COUNT() and SUM(), the use of the GROUP BY clause for grouping records, and the HAVING clause for filtering groups after aggregation. Examples illustrate how to retrieve data based on specific conditions, including filtering by job titles and calculating averages.

Uploaded by

Abhijit Bhatye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

grouping and group-by clause

The document explains group functions in SQL, detailing how they operate on rows to return single values per group. It covers key functions such as COUNT() and SUM(), the use of the GROUP BY clause for grouping records, and the HAVING clause for filtering groups after aggregation. Examples illustrate how to retrieve data based on specific conditions, including filtering by job titles and calculating averages.

Uploaded by

Abhijit Bhatye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Group functions

Function which acts over no of rows and provide single value per group

Whenever no Group by clause is used then whole Recordset(Table) is treated as


single group.

Count() - Returns no of rows, We can use * to represent all columns or we can


specify the column.

> Select count(*) from employee;


Returns no of records in employee table

> select count(comm) from employee;


Returns no of records in employee table where comm is not
null

Sum() - Sum of values of given columns in rows

> select sum(salary) from employee;

Where filtering with group function

Where filtering executes before evaluation of group function so first


records get filtered and the group function will acts over ramaining records

Display Total salary of all Clerks

> Select sum(sal) from emp where job='Clerk';

Group By Clause

It is used to form groups of records as per given columns.

It executes after where filtering and before evaluation of group function

eg.

Display maximum value from each deptn

> Select deptno, Max(sal) from emp group by deptno;


grouping is possible with multiple columns

Display maximum value from each dept and each job

> Select deptno, job, Max(sal) from emp group by deptno,job;

filtering can be used with grouping

Display no of clerks from each dept

> Select deptno, count(*) from emp where job='Clerk' group by deptno;

HAVING Clause

Having is used for group filtering which get executed after grouping
It exists with group by only

eg.

Dispaly All the deptno where avg of salary is greater than 2000

Select deptno,avg(sal) from emp group by deptno having avg(sal)>=2000;

Display Mgr whose no. of subordinates are greater than 3

Display name of the months where more than 3 employees are hired

You might also like