Lab 5
Lab 5
Faculty of Engineering
Department of Communication and Computer Engineering
Lab Objectives:
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:
Dept
dept_cd dept_name
Submission
Submit a report which includes the solution for the above exercise including the query and the SQL
Data result