You are on page 1of 7

Bahria University, Lahore Campus

Department of Computer Sciences


Lab Manual 09
(Fall 2022)

Course: Database Management System Lab


Course Code: CSL 220 Max Marks: 40
Faculty’s Name: Lab Engineer: Shoaib Khan

Name: Umer Arif Enroll No: 03-134211-029

Objectives:
The objective of this lab session is to know what is Group by and having
along with some aggregate function.
By the end of this lab students will be able
 To understand what is Group by clause
 To understand what is having clause
 To use group by with select statement
 To understand the difference between having and where clause

Group By Clause
The GROUP BY Clause is utilized in SQL with the SELECT statement to organize similar
data into groups. It combines the multiple records in single or more columns using some
functions. Generally, these functions are aggregate functions such as min(),max(),avg(),
count(), and sum() to combine into single or multiple columns.

Points to Remember:

 GROUP BY Clause is utilized with the SELECT statement.


 GROUP BY aggregates the results on the basis of selected column: COUNT, MAX, MIN,
SUM, AVG, etc.
 GROUP BY returns only one result per group of data.
 GROUP BY Clause always follows the WHERE Clause.
 GROUP BY Clause always precedes the ORDER BY

Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Having Clause

HAVING Clause utilized in SQL as a conditional Clause with GROUP BY Clause. This
conditional clause returns rows where aggregate function results matched with given
conditions only. It added in the SQL because WHERE Clause cannot be combined with
aggregate results, so it has a different purpose. The primary purpose of the WHERE Clause is
to deal with non-aggregated or individual records.

 HAVING Clause always utilized in combination with GROUP BY Clause.


 HAVING Clause restricts the data on the group records rather than individual records.
 WHERE and HAVING can be used in a single query.

Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

Page 2 of 7
In above example, Table is grouped based on DeptID column and these grouped rows filtered
using HAVING Clause with condition AVG(Salary) > 3000.

Aggregate Functions

Aggregate functions used to combine the result of a group into a single such as COUNT,
MAX, MIN, AVG, SUM, STDDEV, and VARIANCE. These functions also known as
multiple-row functions.

 SUM(): Returns the sum or total of each group.


 COUNT(): Returns the number of rows of each group.
 AVG(): Returns the average and mean of each group.
 MIN(): Returns the minimum value of each group.
 MAX(): Returns the minimum value of each group.

Compare Having and Where Clause in SQL

 In some cases, you need to filter out the individual records. In such cases, you can use
WHERE Clause, Whereas in other cases you need to filter the groups with the specific
condition. In such cases, you can use HAVING Clause.
 WHERE Clause filters the records tuple by tuple while HAVING Clause filters the whole
group.
 A query may have both the clauses( WHERE and HAVING Clause).
 Where Clause applied first and then Having Clause.
 WHERE Clause restricts records before GROUP BY Clause, whereas HAVING Clause
restricts groups after GROUP BY Clause are performed.
 WHERE Clause can be utilized with SELECT, UPDATE, DELETE, and INSERT, whereas
HAVING can be utilized only with SELECT statement.

Page 3 of 7
Lab Tasks:

Consider the following customer table

CustID CustName Age Contact Address City PostCode Country

1 Ali 25 Asad 252, Block H Lahore 12209 Pakistan

2 Akbar 21 Aslam 221, Block A Islama 05021 Pakistan


bad

3 Rizwan 32 Burhan 101, Block B Karachi 05023 Mexico

4 Around the 25 Thomas 120 London WA1 UK


Horn Hardy Hanover Sq. 1DP

5 Berglunds 23 Christina Berguvsvag London S-958 UK


snabbkop Berglund en 22

Page 4 of 7
Task1:
Insert atleast 4 records of the customers for each age group.
Insert atleast 4 records of customers for each country group.
Insert atleast 4 records of the customers for each city group.

create table customer (


custID int,
custName varchar(25),
Age int,
Contact varchar(100),
Addr varchar(100),
City varchar(25),
PostCode int,
Country varchar(25)
);

-- Task 1
insert into customer values
(1,'Ali',25,'Asad','252, Block H','Lahore',12209,'Pakistan'),
(2,'Akbar',21,'Aslam','221, Block A','Islamabad',05021,'Pakistan'),
(3,'Rizwan',32,'Aslam','101, Block B','Karachi',05023,'Mexico'),
(4,'Around the Horn',25,'Thomas Hardy','120 Hanover Sq.','London',06089,'UK'),
(5,'Berglunds snabbkop',23,'Christina Berglund','Berguvsvagen ','London',06079,'UK')

insert into customer values


(6,'Sami',33,'Awan','Johar Town','Lahore',06089,'Pakistan'),
(7,'Muqeet',35,'Abdul','Jaloo Park','Lahore',06089,'Pakistan'),
(8,'John',35,'Parker','Clover Street','London',06072,'UK'),
(9,'Tom',35,'Cruise','LiverPool','London',06072,'UK')

insert into customer values


(10,'Naruto',40,'Uzumaki','Johar Town','Tokyo',54000,'Japan'),
(11,'Sasuke',42,'Uchicha','Jaloo Park','Tokyo',54000,'Japan'),
(12,'Kakashi',45,'Hatake','Clover Street','Tokyo',54000,'Japan'),
(13,'Sakura',47,'Haruno','LiverPool','Tokyo',54000,'Japan')

Task 2
List the number of customers in each country

SELECT Country,COUNT(custID) as NoofCustomers


FROM customer
GROUP BY Country

Task 3
List the number of customers in each country, sorted high to low
List the number of customer in each country with city as Lahore.

SELECT Country,COUNT(custID) as NoofCustomers


FROM customer
GROUP BY Country

Page 5 of 7
ORDER BY COUNT(custID) DESC;

SELECT Country,COUNT(custID) as NoofCustomers


FROM customer
WHERE City = 'Lahore'
GROUP BY Country

Task 4
List the number of customers in each country. Only include countries with more than 2
customers.

SELECT Country,COUNT(custID) as NoofCustomers


FROM customer
GROUP BY Country
Having COUNT(custID) > 2

Task 5
List the number of customers in each country, sorted high to low (Only include countries with
more than 5 customers):

SELECT Country,COUNT(custID) as NoofCustomers


FROM customer
GROUP BY Country
Having COUNT(custID) > 5
ORDER BY COUNT(custID) DESC;

Task 6
List the details of the customer of similar age count more than 2.
SELECT Country, COUNT(Age) as NoofCustomers
FROM customer
GROUP BY Country

Page 6 of 7
Lab Grading Sheet :
Max
Obtained
Task Mark Comments(if any)
Marks
s
1. 10
2. 10
3. 10
4. 10

Total 40 Signature

Note : Attempt all tasks and get them checked by your Instructor

Page 7 of 7

You might also like