You are on page 1of 2

COMPUTER EDUCATION

CLASS VII
CHAPTER – 7

Till now we have learnt to work with functions that operate on individual rows in a
table. SQL also supports and provides group functions or aggregate functions.
Group functions work upon groups of rows rather than on single row. That is why
these functions are sometimes called multiple row functions.

The Group By : The GROUP BY clause combines all those records that have
common values in particular field or a group of fields. In other words, the GROUP BY
clause is used in SELECT statements to divide the table into groups. The GROUP
BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM,
AVG) to group the result set by one or more columns.

Syntax: - select <field name>, function (<field name>) from <table name> group by
<field name>;

1. Example: - SELECT JOB, COUNT(JOB) FROM EMP GROUP BY JOB;

Output: -

JOB COUNT(JOB)
ANALYST 2
CLERK 4
MANAGER 3
PRESIDENT 1
SALESMAN 4
2. Example: -SELECT SUM(SAL), DEPTNO FROM EMP GROUP BY DEPTNO;

SUM(SAL) DEPTNO
8750 10
10875 20
9400 30
Having clause: -

The HAVING clause places conditions on groups in contrast to WHERE clause that
places condition on individual rows. While WHERE condition cannot include
aggregate functions, HAVING condition can do so.

Syntax: - select <field name>, function(<field name>) from <table name> group by
<field name> having condition;
1. Example: - SELECT JOB, COUNT (JOB) FROM EMP GROUP BY JOB HAVING
COUNT (JOB) > 1;
JOB COUNT(JOB)
ANALYST 2
CLERK 4
MANAGER 3
SALESMAN 4
2. Example: -
SELECT SUM(SAL), DEPTNO FROM EMP GROUP BY DEPTNO HAVING SUM
(SAL) > 9500;

SUM(SAL) DEPTNO
10875 20

You might also like