0% found this document useful (0 votes)
70 views

Department of Computer Science: Lab 06: Aggregating Data Using Group Functions

This document describes a database systems lab on aggregating data using group functions in SQL. The lab introduces common group functions like SUM, COUNT, MIN, and MAX and shows how to group query results by one or more columns using the GROUP BY clause. It also demonstrates how to filter grouped results using the HAVING clause. Students are instructed to practice examples of group functions and queries using the GROUP BY and HAVING clauses. They are then given tasks to write SQL queries using these concepts to retrieve aggregated information from database tables.

Uploaded by

Mydah Nasir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Department of Computer Science: Lab 06: Aggregating Data Using Group Functions

This document describes a database systems lab on aggregating data using group functions in SQL. The lab introduces common group functions like SUM, COUNT, MIN, and MAX and shows how to group query results by one or more columns using the GROUP BY clause. It also demonstrates how to filter grouped results using the HAVING clause. Students are instructed to practice examples of group functions and queries using the GROUP BY and HAVING clauses. They are then given tasks to write SQL queries using these concepts to retrieve aggregated information from database tables.

Uploaded by

Mydah Nasir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Department of Computer Science

CS220: Database Systems

Class: BSCS-10AB
Lab 06: Aggregating Data Using Group Functions

Date: 1st November, 2021


Time: 9:00-12:00 & 02:00-05:00
Instructor: Dr. Munir & Dr. Shah Khalid

Lab Engineer: Anum Asif

CLO-2: Formulate SQL queries to retrieve information from a relational database.


Lab 06: Aggregating Data Using Group Functions
Introduction
The GROUP BY clause can be used in a SELECT statement to collect data across multiple
records and group the results by one or more columns.

Relational Algebra is a meta-language and forms underlying basis of SQL query language. It has
six basic operators including: select, project, union, set difference, rename, and cross product.
The operators take one or two relations as inputs and produce a new relation as a result.

Objectives
After completing this lab, you should be able to do the following:

 Identify the available group functions


 Describe the use of group functions
 Group data using the GROUP BY clause
 Include or exclude grouped rows by using the HAVING clause
 Extracting data from multiple tables

Tools/Software Requirement
MySQL workbench

Description
This lab further addresses functions. It focuses on obtaining summary information, such as
averages, for groups of rows. It discusses how to group rows in a table into smaller sets and how
to specify search criteria for groups of rows.

Instructions
Execute the company.sql script to create company schema first. After that, practice the given
examples and all group functions of SQL. At the end, attempt the questions given as lab tasks in
the manual.

NOTE:

Reference link for database creation :

https://justinsomnia.org/2009/04/the-emp-and-dept-tables-for-mysql/
SQL Group by Clause:
Lab Practice 1: using the SUM function

For example, you could also use the SUM function to return the department-id and the total
salary (in the associated department).

SELECT deptno, SUM(sal) as "Total salary"


FROM emp
GROUP BY deptno;

Because you have listed one column in your SELECT statement that is not encapsulated in the
SUM function, you must use a GROUP BY clause. The department field must, therefore, be
listed in the GROUP BY section.

Lab Practice 2: using the COUNT function

For example, you could use the COUNT function to return the department-id and the number of
employees (in the associated department) that make over $25,000 / year.

SELECT deptno, COUNT(*) as "Number of employees"


FROM emp
WHERE sal > 2500
GROUP BY deptno;

Lab Practice 3: using the MIN function

For example, you could also use the MIN function to return the department-id and the minimum
salary in the department.

SELECT deptno, MIN(sal) as "Lowest salary"


FROM emp
GROUP BY deptno;

Lab Practice 4: using the MAX function

For example, you could also use the MAX function to return the department-id and the
maximum salary in the department.

Formulate the query yourself.


SQL : Having Clause
Lab Practice 5: using the SUM function

For example, you could also use the SUM function to return the department-id and the total sales
(in the associated department). The HAVING clause will filter the results so that only
departments with sales greater than $1000 will be returned.

SELECT deptno, SUM(sal) as "Total sales"


FROM emp
GROUP BY deptno
HAVING SUM(sal) > 10000;

Lab Practice 6: using the COUNT function

For example, you could use the COUNT function to return the name of the department and the
number of employees (in the associated department) that make over $25,000 / year. The
HAVING clause will filter the results so that only departments with more than 10 employees will
be returned.

SELECT deptno, COUNT(*) as "Number of employees"


FROM emp
WHERE sal > 2500
GROUP BY deptno
HAVING COUNT(*) > 1;

Lab Practice 7: using the MAX function

For example, you could also use the MAX function to return the id of each department and the
maximum salary in the department. The HAVING clause will return only those departments
whose maximum salary is less than $50,000.

SELECT deptno, MAX(sal) as "Highest salary"


FROM emp
GROUP BY deptno
HAVING MAX(sal) < 5000;

ALIAS

SQL aliases are used to temporarily rename a table or a column heading.


SQL Aliases

SQL aliases are used to give a database table, or a column in a table, a temporary name.
Basically aliases are created to make column names more readable.

SQL Alias Syntax for Columns


SELECT column_name AS alias_name
FROM table_name;

SQL Alias Syntax for Tables


SELECT column_name(s)
FROM table_name  AS alias_name;

Example:

SELECT first_name AS Customer

FROM sakila.customer;

AS keyword is optional, even if you remove it a column or table alias is formulated.

Lab TASKS:
Formulate SQL queries for following information needs and execute them in MySQL
server.

 Find the highest, lowest, sum and average salary of all employees. Label the columns as
Maximum, Minimum, Sum and Average respectively. Save your query.
Query:

select max(sal) as Maximum, min(sal) as Minimum, sum(sal) as Sum, avg(sal) as Average

from emp;

Screenshot:
 Find the highest, lowest, sum and average salary for each job type. Label the columns as
Maximum, Minimum, Sum and Average respectively. Save your query.
Query:

select max(sal) as Maximum, min(sal) as Minimum, sum(sal) as Sum, avg(sal) as Average, job

from emp group by job;

Screenshot:

 Lists the number of employees in each job, sorted high to low.


Query:

select count(*) as 'Number of Employees', job from emp group by job order by 'Number of
Employees';

Screenshot:
 Display the number of distinct department values in the EMPLOYEES table.
Query:
select count( distinct deptno) as 'Number of distinct department values' from emp;

Screenshot:

 Determine the number of managers without listing them. Label the column as Number of
Mangers.
Query:
select count(*) AS 'Number of Managers' from emp where job = 'MANAGER';

Screenshot:
 Find the difference between highest and lowest salaries.
Query:
select (max(sal) - min(sal)) as 'Difference' from emp;

Screenshot:

 Give the department names and their locations.


Query:
select dname, loc from dept;

Screenshot:

 Retrieve total no. of employees in the Company.


Query:
select count(*) as 'Number of Employees' from emp;

Screenshot:
Deliverables

Save all queries and their results in the word document that you are executing, including
examples and the questions. Relational algebra queries expressions save in plain text file or
word doc .Upload this document to LMS. Late submissions will not be accepted.

You might also like