Lab 07
Lab 07
from emp
where job='manager'
group by job
having min(sal)>6000
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/
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.
SELECT deptno, MIN(sal) as "Lowest salary"
FROM emp
GROUP BY deptno;
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.
SELECT deptno, MAX(sal) as "Highest salary"
FROM emp
GROUP BY deptno
HAVING MAX(sal) < 50000;
ALIAS
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.
Example:
FROM sakila.customer;
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.