0% found this document useful (0 votes)
33 views2 pages

DBMS Lab 3 Tasks

Lab 3 of CS-232 DBMS focuses on SQL clauses: GROUP BY, HAVING, and ORDER BY, teaching students to aggregate, filter, and sort data in SQL queries. Students will practice creating tables, inserting records, and writing queries to analyze data using these clauses. The lab aims to enhance proficiency in advanced data analysis and reporting tasks within a relational database management system.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views2 pages

DBMS Lab 3 Tasks

Lab 3 of CS-232 DBMS focuses on SQL clauses: GROUP BY, HAVING, and ORDER BY, teaching students to aggregate, filter, and sort data in SQL queries. Students will practice creating tables, inserting records, and writing queries to analyze data using these clauses. The lab aims to enhance proficiency in advanced data analysis and reporting tasks within a relational database management system.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

CS-232 DBMS Lab #3 Spring 2025

Lab 3 –SQL CLAUSES-


ORDER BY, GROUP BY, HAVING
The objective of this lab is to introduce students to the GROUP BY, HAVING, and ORDER BY clauses
in SQL, which are essential for data aggregation, filtering grouped data, and sorting query results. By
the end of this lab, students will be able to:
1. Understand the purpose and functionality of the GROUP BY clause for grouping rows based on one or
more columns.
2. Use the GROUP BY clause with and without aggregate functions (e.g., COUNT(), SUM(), AVG()) to
analyze and summarize data.
3. Apply the GROUP BY clause with multiple columns to create more granular groupings.
4. Group data based on a date column and perform aggregate operations on grouped data.
5. Use the HAVING clause to filter grouped data based on specified conditions.
6. Combine the HAVING clause with aggregate functions like SUM() to filter grouped results.
7. Understand the purpose of the ORDER BY clause for sorting query results.
8. Use the ORDER BY clause to sort rows by one column in ascending or descending order.
9. Sort rows by multiple columns using the ORDER BY clause to organize data hierarchically.
Through hands-on practice, students will gain proficiency in writing SQL queries to group, filter, and sort data,
enabling them to perform advanced data analysis and reporting tasks in a relational database management
system (RDBMS).

1. Using GROUP BY Without an Aggregate Function


o Create a table named Employees with the following columns:
 EmployeeID (INT, Primary Key)
 FirstName (VARCHAR(50))
 LastName (VARCHAR(50))
 Department (VARCHAR(50))
 Salary (INT)
o Insert at least 10 records into the Employees table.
o Use the GROUP BY clause to group records by the Department column. Observe the output
and explain why this is not useful without an aggregate function.
2. Using GROUP BY with COUNT() Function
o Write a query to count the number of employees in each department using the GROUP
BY clause and the COUNT() function.

Task 2: GROUP BY with Multiple Columns


3. Using GROUP BY with Multiple Columns
o Add a JoiningDate column (DATE) to the Employees table.
o Insert additional records to ensure some employees have the same Department and JoiningDate.
o Write a query to group records by both Department and JoiningDate and count the number of
employees in each group.

Task 3: GROUP BY with a Date Column


4. Using GROUP BY with a Date Column
o Write a query to group records by the JoiningDate column and calculate the total salary for each
group.
Task 4: Introduction to HAVING Clause
5. HAVING Clause with SUM Function
o Write a query to calculate the total salary for each department using the GROUP BY clause.
o Use the HAVING clause to filter out departments where the total salary is greater than 50000.

Task 5: Introduction to ORDER BY Clause


6. ORDER BY to Sort Rows by One Column
o Write a query to display all employees sorted by their Salary in descending order.
7. ORDER BY to Sort Rows by Multiple Columns
o Write a query to display all employees sorted first by Department in ascending order and then
by Salary in descending order.

Task 6: Combined Practice


8. Combining GROUP BY, HAVING, and ORDER BY
o Write a query to:
 Group employees by Department.
 Calculate the average salary for each department.
 Filter out departments where the average salary is less than 30000 using
the HAVING clause.
 Sort the results by the average salary in descending order.

Sample Data:
EmployeeID FirstNam LastName Departmen Salary JoiningDate
e t
1 John Doe HR 40000 2023-01-15
2 Jane Smith IT 50000 2023-02-10
3 Alice Johnson HR 45000 2023-01-15
4 Bob Brown Finance 60000 2023-03-05
5 Charlie Davis IT 55000 2023-02-10
6 David Wilson Finance 70000 2023-03-05
7 Eve Taylor HR 48000 2023-01-15
8 Frank Moore IT 52000 2023-02-10
9 Grace Lee Finance 65000 2023-03-05
10 Henry Clark IT 53000 2023-02-10

You might also like