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

Lab 5

This document provides an overview of advanced SQL queries for a database systems lab. It describes querying multiple tables, nested queries, joined queries, aggregate functions, and join conditions. Students are asked to write SQL statements to retrieve employee names from the administration department ordered by salary, update salaries in that department by 10%, display departments with average salaries over $40,000 showing department details and employee counts, and more complex multi-table queries.

Uploaded by

ibrahim zayed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Lab 5

This document provides an overview of advanced SQL queries for a database systems lab. It describes querying multiple tables, nested queries, joined queries, aggregate functions, and join conditions. Students are asked to write SQL statements to retrieve employee names from the administration department ordered by salary, update salaries in that department by 10%, display departments with average salaries over $40,000 showing department details and employee counts, and more complex multi-table queries.

Uploaded by

ibrahim zayed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

TAFILA TECHNICAL UNIVERSITY

Faculty of Engineering
Department of Communication and Computer Engineering

Database Systems Lab


Lab 5
Advanced Data Retrieval Operations
______________________________________________________________________

Lab Objectives:

- Write and understand advanced SQL Queries.


- Topics to be covered include querying multiple tables, nested queries, joined queries, joined
tables, and aggregate functions.

Introduction:

This lab is to practice writing complex SQL queries. All the material in this lab is covered in class.
The following section gives an overview of the covered topics, but it does not review the syntax for
using all the advanced SQL options. Therefore, you are encouraged to bring your book or class-notes
to the lab in case you needed to review the syntax for some of the SQL operations.

Overview:

Writing a query is not always a simple task; queries may be simple involving only one table or may
be complex involving multiple tables. A query is usually written in response to a certain request that
specifies the conditions which should be satisfied in the retrieved data. Usually conditions can be
classified into "select conditions" and “join conditions”. The former type of conditions is used to
filter-out tuples from a table, while the latter is used to join two tables, or equivalently, find matching
tuples. Usually, a query that involves (N) base relations should have (N-1) join conditions in its
"where" clause.

Sometimes a complex query can be simplified by dividing it into two or more nested queries. Also, if
the value of an attribute is to be compared with a set of values then we may need to use nested
queries. Nested queries are said to be correlated if the condition in the inner query refers to the tuple
under consideration in the outer query. Otherwise, we say that the nested queries are uncorrelated.
The output of the inner query is always the same if the queries are uncorrelated.

If a query involves multiple relations then these tables must be joined properly for the query to be
correct, join conditions usually compare a foreign key to a primary key. Joins can be used implicitly
by including the join condition in the where clause, or they can be used explicitly by using the
"JOIN" operator with the corresponding join condition in the "FROM" clause. To include unmatched
tuples in one or both of the joined relations outer joins can be used.
Exercise:

Consider the following relational schema:


Emp

emp_no name salary supervisor_no sex_code dept_code

Dept
dept_cd dept_name

Write SQL statement for the following queries:

a) Display Employee names of Administration department ordered by their salary.


b) Write a DML statement that will update the salary for employees in the administration department
by ten percent.
Ex. if salary was 30000 it will be 33000, 40000 will be 440000, 50000 will be 55000 and so on.
c) Display Department Number, Department Name, and Number of Employees for all departments
which have average salary greater than 40000.
d) Display Department Number, Department Name, and Number of Employees for all departments
which have average salary greater than administration department average salary.
e) Display Department Number, Department Name, and Number of Employees for the departments
which have the largest number of employees.
f) Display Employees getting more salary than their supervisor.
g) Display Name of employee(s) who earn maximum salary in their organization.

Submission
Submit a report which includes the solution for the above exercise including the query and the SQL
Data result

You might also like