Department of Computer Science: Lab 06: Aggregating Data Using Group Functions
Department of Computer Science: Lab 06: Aggregating Data Using Group Functions
Class: BSCS-10AB
Lab 06: Aggregating Data Using Group Functions
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:
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:
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).
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.
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.
For example, you could also use the MIN function to return the department-id and the minimum
salary in the department.
For example, you could also use the MAX function to return the department-id and the
maximum salary in the department.
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.
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.
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.
ALIAS
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.
Example:
FROM sakila.customer;
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:
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
Screenshot:
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:
Screenshot:
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.