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

Lab 07

This document provides instructions for a database systems lab focusing on aggregating data using group functions in SQL. The lab introduces group functions like SUM, COUNT, MIN, and MAX and shows how to group data by columns. It also covers the HAVING clause to filter groups. Students are asked to execute example queries using these functions and clauses, and to formulate queries to answer tasks extracting various summary data from database tables.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Lab 07

This document provides instructions for a database systems lab focusing on aggregating data using group functions in SQL. The lab introduces group functions like SUM, COUNT, MIN, and MAX and shows how to group data by columns. It also covers the HAVING clause to filter groups. Students are asked to execute example queries using these functions and clauses, and to formulate queries to answer tasks extracting various summary data from database tables.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Department of Mathematics

CS220: Database Systems

Class: BS Mathematics Semester 4


Lab 07: Aggregating Data Using Group Functions

Date: April 09, 2021


Time: 02:00-05:00
Instructor: Ms. Naheeda Parveen

Lab Engineer: Sundas Dawood

Select mgr,min(sal) as MinimumSalary

from emp

where job='manager'

group by job

having min(sal)>6000

order by mgr desc;


Lab 05: 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 > 25000
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) > 1000;

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 > 25000
GROUP BY deptno
HAVING COUNT(*) > 10;

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) < 50000;

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.

Retrieving data from multiple tables:

To retrieve data from multiple tables use the following syntax:

Select col1, col2, col3,


From Relation R1, Relation R2, Relation R3….Relation Rn
Where condition;
Example:

Select EMP_name, EMP_Job,EMP_Salary


From Employee, Job
Where Employee.E_ID = Job.E_ID;

In this table two relations are joined and a join condition is applied.

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.
 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.
 Lists the number of employees in each job, sorted high to low.
 Display the number of distinct department values in the EMPLOYEES table.
 Determine the number of managers without listing them. Label the column as Number of
Mangers.
 Find the difference between highest and lowest salaries.
 Formulate a query to display the manager number and the salary of the lowest-paid
employee for that manager. Exclude any groups where the minimum salary is 6000 or
less. Sort the output in descending order of salary.
 Give the department names and their locations.
 Retrieve total no. of employees in the Company.
 Practice retrieving data from multiple tables.

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