Professional Documents
Culture Documents
03 134211 029 10290789161 13122022 063600pm
03 134211 029 10290789161 13122022 063600pm
03 134211 029 10290789161 13122022 063600pm
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:
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.
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.
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:
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.
-- 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')
Task 2
List the number of customers in each 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.
Page 5 of 7
ORDER BY COUNT(custID) DESC;
Task 4
List the number of customers in each country. Only include countries with more than 2
customers.
Task 5
List the number of customers in each country, sorted high to low (Only include countries with
more than 5 customers):
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