You are on page 1of 17

4

GROUP BY CLAUSE , HAVING CLAUSE,


MULTIPLE ROW ( OR GROUP OR
AGGREGATE) FUNCTIONS
&
ORDER BY CLAUSE [ CLASS XI NOTES ]

GROUP BY CLAUSE
At times we need to fetch a group of rows on the basis of common values in a
column. This can be done using a GROUP BY clause. It groups the rows
together that contain the same values in a specified column. We can use the
aggregate functions (COUNT, MAX, MIN, AVG and SUM) to work on the
grouped values. HAVING Clause in SQL is used to specify conditions on the rows
with GROUP BY clause.
The GROUP BY clause divides the table into various groups, according to the
column specified.
For dividing the table into groups, the column should have repeating values.

The GROUP BY Clause is used along with the group functions to retrieve data
grouped according to one or more columns.

HAVING CLAUSE ( PLACING CONDITIONS


ON GROUPS ):
Sometimes we do not want to see the whole output produced by a statement with
GROUP BY clause. We want to see the output only for those groups which satisfy
some condition.

Page 1 of 17
It means we want to put some condition on individual groups (and not on
individual records). A condition on groups is applied by HAVING clause.

Having clause is used to filter data based on the group functions. This is similar
to WHERE condition but is used with group functions.

Group functions cannot be used in WHERE Clause but can be used in HAVING
clause.

The HAVING clause was added to SQL because the WHERE keyword cannot
be used with aggregate functions.

Difference between where and having clauses:

Where clause is used to apply condition on individual records and having clause
is used to apply condition on groups created by group by clause.

SELECT column_name(s)
Condition on individual records
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition ; Condition on groups

Table Name - Emp


EmpNo EmpName Dept Salary
1 A Phy 7000
2 B Comp 8000
3 C Commerce 7500
4 D Commerce 6000
5 E Phy 8000
6 F Phy 7000

Page 2 of 17
 Display the maximum of the salary and Deptname according to each and
every dept

Select max(salary), dept from Emp GROUP BY dept;

what to display according to what

Output result:-
Max ( salary ) Dept
7500 Commerce
8000 Comp
8000 Phy

 Display the Dept name and the least salary according to every dept

Select Dept, MIN (salary) from Emp GROUP BY Dept;


Output result:-
Dept Min( salary )
Commerce 6000
Comp 8000
Phy 7000

 Display the total salary paid to each and every Dept

Select sum ( salary ) , Dept from Emp GROUP BY Dept;


Output result:-
Sum( salary ) Dept
13500 Commerce
8000 Comp
22000 Phy

Page 3 of 17
 Display the average of salary paid to each and every dept.

Select AVG(salary) , Dept from emp GROUP BY Dept ;


 Display the no. of records according to each and every dept

Select count (*), dept from Emp GROUP BY Dept;


 Display the no. of employees according to each and every dept, consider
only those dept having atleast 2 employees.

Select count (*) , dept from emp GROUP BY Dept having count (*) >= 2 ;

Note- The WHERE clause should be replaced by HAVING clause in commands


using GROUP functions.

 Find the output of the following MySQL commands-

Table name - Student


RNo Name
1 A
2 B
3
My SQL commands:-
 Select count ( * ) from student ;
Output - 3
 Select count ( name ) from student ;
Output - 2
 Select count ( rno ) from student ;
Output - 3

Page 4 of 17
Note - When using count with column name such as COUNT(name), null
values will not be counted.

Let us consider the following table named EMPL with the below given
structure / data :

( Pls Note : This Table EMPL will be used in the forth coming questions )

MULTIPLE ROW ( OR GROUP OR


AGGREGATE) FUNCTIONS

Aggregate functions work upon groups of rows, rather than on single row.

Various Group Functions are


 AVG( )
 MIN( )
 MAX( )
 SUM( )
 COUNT( )

Page 5 of 17
All group functions accept the following options:

DISTINCT : to consider only distinct values of the argument expression.

ALL: to consider all values including all duplicates

TOPICS TO RECALL :
GROUP BY clause:

GROUP BY clause is used in a SELECT statement in conjunction with


aggregate functions to group the result based on distinct values in a column.

HAVING clause:

HAVING clause is used in conjunction with GROUP BY clause in a


SELECT statement to put condition on groups.

1. AVG( ) :

The AVG( ) function returns the average value of a numeric column.

Syntax -
AVG([DISTINCT | ALL] < col >)

Page 6 of 17
Examples:
a) Display the average salary from EMPL table;

b) Display the department wise average salary from EMPL table;

2. MAX ( ) :

The MAX( ) function returns the largest value of the selected column.

Syntax:
MAX([DISTINCT | ALL] < col >)
Examples:

a) Display the maximum salary from EMPL table;

Page 7 of 17
b) Display the department wise maximum salary from EMPL table;

3. MIN( ) :

The MIN( ) function returns the smallest value of the selected column.

Syntax:
MIN([DISTINCT | ALL] < col >)

Examples:

a ) Display the minimum salary from EMPL table.

b) Display the department wise minimum salary from EMPL table

4. SUM( ) :

The SUM() function returns the total sum of a numeric column.

Page 8 of 17
Examples:

a) Display the sum of salaries

b) Display the department wise sum of salaries

5. COUNT ( ) :

The COUNT() function returns the number of rows that matches a specified
criteria.
Syntax:
COUNT(*) or COUNT([DISTINCT | ALL] < col >)

Examples:

a) Display the total number of employees in EMPL table.

Page 9 of 17
b) Display the department wise total number of employees in EMPL table.

c) Display the total number of commission/COMM values available in EMPL table.

d) Display the total number of unique jobs available.

e) Display the number of employees according to each and every dept, consider
only those depts having more than 2 employees.

Page 10 of 17
DIFFERENCE BETWEEN COUNT(*) AND COUNT(<column name>)
When the argument is a column name or an expression based on a column,
COUNT( ) returns the number of non-NULL values in that column.
If the argument is *, then COUNT(*) counts the total number of rows in the
table including duplicates and nulls.

Refer below given example:-

Aggregate functions and NULL values:

None of the aggregate functions takes NULL into consideration. NULL is


simply ignored by all the aggregate functions.

---------------------------------------------------------------------------------------------------

Page 11 of 17
QN : OBSERVE THE BELOW GIVEN TABLE XIIB AND ANSWER THE
FOLLOWING QUESTIONS :

a) Select sum(age) , avg(age) from XIIB;

Calculation : Eventhough there are SEVEN( 7 ) records in the table XIIB , 2


records are having NULL values in the age column.

So , When calculating avg(age) , the NULL values are not taken into
consideration.
Therefore , the calculation is like this:
The sum of the age column is 76 , and the number of records is 5 ( 7 – 2 null =
5)

So , avg(age) = 76 / 5 = 15.2

Page 12 of 17
b) select count(*) , count(age) from XIIB;

EXAMPLE 1:

Page 13 of 17
1) Display the number of books available in each booktype
Ans :

2) Display the number of books available in each booktype , consider the


book type that has more than two books.
Ans :
Select booktype , count(*) from library group by booktype where count(*) > 2;
Error :

Reason for Error : Aggregate Functions / Group functions ( count(*) in this


example ) cannot be used in where condition

Corrected Answer :

Page 14 of 17
3) Display the total number of books available under text book and novel
category.

4) Display the total number of books available under each book type ,
consider only those books which cost 300 or more.

EXAMPLE 2:

Consider the SALES table from the CARSHOWROOM database.

Page 15 of 17
mysql> SELECT * FROM SALES;

1) Display the number of cars purchased by each customer from the SALES table.
mysql> SELECT CustID, COUNT(*) "Number of Cars" FROM SALES GROUP BY
CustID;

2) Display the customer Id and number of cars purchased if the customer


purchased more than 1 car from SALES table.

mysql> SELECT CustID, COUNT(*) FROM SALES GROUP BY CustID


HAVING Count(*) > 1;

Page 16 of 17
3) Display the number of people in each category of payment mode from the table
SALES ( sorted in the ascending order for Paymentmode column).

mysql> SELECT PaymentMode, COUNT(*) FROM SALES GROUP BY


Paymentmode ORDER BY Paymentmode ;

4) Display the PaymentMode and number of payments made using that mode
more than once.

mysql> SELECT PaymentMode, Count(*) FROM SALES GROUP BY


Paymentmode HAVING COUNT(*) > 1 ;

ORDER BY CLAUSE :
REFER FROM CLASS XI NOTES ..

************* COMPLETED THE ABOVE TOPIC ***************


Note :
1. The above mentioned should be written in the Informatics Note book as a
continuity of the previous notes. ( No Printout Allowed )
2. The entire topic will be discussed in the class.
3. Mistakes / corrections ( if any ) will be rectified during class room discussion.
******************************************************************

Page 17 of 17

You might also like