Lab 04
Lab 04
Class: BESE-14AB
Lab 04: 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;
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;
Result:
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 job, MAX(sal) AS "Maximum", MIN(sal) AS "Minimum", SUM(sal) AS
"Sum", AVG(sal) AS "Average"
FROM emp
GROUP BY job;
Result:
Lists the number of employees in each job, sorted high to low.
Query:
SELECT job, COUNT(*) AS "No. of Employees" FROM emp
GROUP BY job ORDER BY "No. of Employees" DESC;
Result:
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';
Result:
Find the difference between highest and lowest salaries.
Query:
SELECT MAX(sal) - MIN(sal) AS "difference"
FROM emp;
Result:
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 4000 or
less. Sort the output in descending order of salary.
Query:
SELECT mgr AS "Manager Number", MIN(sal) AS "Lowest Salary"
FROM emp
GROUP BY mgr
HAVING MIN(sal) > 4000
ORDER BY "Lowest Salary" DESC;
Result:
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.