Professional Documents
Culture Documents
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.
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.
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
Page 2 of 17
Display the maximum of the salary and Deptname according to each and
every dept
Output result:-
Max ( salary ) Dept
7500 Commerce
8000 Comp
8000 Phy
Display the Dept name and the least salary according to every dept
Page 3 of 17
Display the average of salary paid to each and every dept.
Select count (*) , dept from emp GROUP BY Dept having count (*) >= 2 ;
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 )
Aggregate functions work upon groups of rows, rather than on single row.
Page 5 of 17
All group functions accept the following options:
TOPICS TO RECALL :
GROUP BY clause:
HAVING clause:
1. AVG( ) :
Syntax -
AVG([DISTINCT | ALL] < col >)
Page 6 of 17
Examples:
a) Display the average salary from EMPL table;
2. MAX ( ) :
The MAX( ) function returns the largest value of the selected column.
Syntax:
MAX([DISTINCT | ALL] < col >)
Examples:
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:
4. SUM( ) :
Page 8 of 17
Examples:
5. COUNT ( ) :
The COUNT() function returns the number of rows that matches a specified
criteria.
Syntax:
COUNT(*) or COUNT([DISTINCT | ALL] < col >)
Examples:
Page 9 of 17
b) Display the department wise total number of employees in EMPL table.
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.
---------------------------------------------------------------------------------------------------
Page 11 of 17
QN : OBSERVE THE BELOW GIVEN TABLE XIIB AND ANSWER THE
FOLLOWING QUESTIONS :
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 :
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:
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;
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).
4) Display the PaymentMode and number of payments made using that mode
more than once.
ORDER BY CLAUSE :
REFER FROM CLASS XI NOTES ..
Page 17 of 17