You are on page 1of 31

Week 7

---------------- WEEK 14

Relational Algebra and SQL


(Set Operation and Nested Queries)

Pearson Education © 2009


Objectives
 Relational Algebra on Set Operation:
(Union, Difference and Intersection)

 SQL on Union, Difference and Intersection


 Sub-query and Nested queries.

2
Pearson Education © 2009
Relational Algebra Operations

3
Pearson Education © 2009
Relational Algebra Operations

4
Union
 RS
– Union of two relations R and S defines a relation
that contains all the tuples of R, or S, or both R
and S, duplicate tuples being eliminated.
– R and S must be union-compatible.

 If R and S have I and J tuples, respectively, union


is obtained by concatenating them into one relation
with a maximum of (I + J) tuples.

5
Pearson Education © 2009
Example - Union
 List all cities where there is either a branch office
or a property for rent.

city(Branch)  city(PropertyForRent)

6
Pearson Education © 2009
Set Difference
R –S
– Defines a relation consisting of the tuples that
are in relation R, but not in S.
– R and S must be union-compatible.

7
Pearson Education © 2009
Example - Set Difference
 Listall cities where there is a branch office but no
properties for rent.

city(Branch) – city(PropertyForRent)

8
Pearson Education © 2009
Intersection
R S
– Defines a relation consisting of the set of all
tuples that are in both R and S.
– R and S must be union-compatible.

 Expressed using basic operations:


R  S = R – (R – S)

9
Pearson Education © 2009
Example - Intersection
 Listall cities where there is both a branch office
and at least one property for rent.

city(Branch)  city(PropertyForRent)

10
Pearson Education © 2009
SQL on Union, Intersection
and Difference

11
Example : SQL on UNION
List all cities where there is either a branch office or
a property.

(SELECT city FROM Branch)


UNION
(SELECT city FROM PropertyForRent);


12
Pearson Education © 2009
Example : SQL on INTERSECTION
List all cities where there is both a branch office and
a property.

(SELECT city FROM Branch)


INTERSECT
(SELECT city FROM PropertyForRent);


13
Pearson Education © 2009
Example : SQL on DIFFERENCE
List of all cities where there is a branch office but no
properties.
(SELECT city FROM Branch)
MINUS
(SELECT city FROM PropertyForRent);

-
14
Pearson Education © 2009
Sub-query and Nested query

15
Subqueries
 Some SQL statements can have a SELECT
embedded within them.
A subselect can be used in WHERE and
HAVING clauses of an outer SELECT, where it
is called a subquery or nested query.
 Subselectsmay also appear in INSERT,
UPDATE, and DELETE statements.

16
Pearson Education © 2009
Example : Subquery with Equality
List staff who work in branch at ‘163 Main St’.

SELECT staffNo, fName, lName, position


FROM Staff
WHERE branchNo =
(SELECT branchNo
FROM Branch
WHERE street = ‘163 Main St’);

17
Pearson Education © 2009
Example : Subquery with Equality
 Inner SELECT finds branch number for branch
at ‘163 Main St’ (‘B003’).
 Outer SELECT then retrieves details of all staff
who work at this branch.
 Outer SELECT then becomes:

SELECT staffNo, fName, lName, position


FROM Staff
WHERE branchNo = ‘B003’;

18
Pearson Education © 2009
Example : Subquery with Equality

19
Pearson Education © 2009
Example : Subquery with Aggregate
List all staff whose salary is greater than the average
salary, and show by how much.

SELECT staffNo, fName, lName, position,


salary – (SELECT AVG(salary) FROM Staff) As SalDiff
FROM Staff
WHERE salary >
(SELECT AVG(salary)
FROM Staff);

20
Pearson Education © 2009
Example : Subquery with Aggregate
 Cannot write ‘WHERE salary > AVG(salary)’
 Instead, use subquery to find average salary
(17000), and then use outer SELECT to find those
staff with salary greater than this:

SELECT staffNo, fName, lName, position,


salary – 17000 As salDiff
FROM Staff
WHERE salary > 17000;

21
Pearson Education © 2009
Example : Subquery with Aggregate

22
Pearson Education © 2009
Subquery Rules
 ORDER BY clause may not be used in a
subquery (although it may be used in outermost
SELECT).
 Subquery SELECT list must consist of a single
column name or expression, except for
subqueries that use EXISTS.
 By default, column names refer to table name in
FROM clause of subquery. Can refer to a table
in FROM clause of outer query using an alias.
23
Pearson Education © 2009
Subquery Rules
 When subquery is an operand in a comparison,
subquery must appear on right-hand side.

The SQL below is invalid:

SELECT staffNo, fName, lName, position,


salary – (SELECT AVG(salary) FROM Staff) As SalDiff
FROM Staff
WHERE (SELECT AVG(salary) FROM Staff) < salary ;

24
Pearson Education © 2009
Example : Nested subquery: use of IN
List properties handled by staff at ‘163 Main St’.
SELECT propertyNo, street, city, postcode, type, rooms, rent
FROM PropertyForRent
WHERE staffNo IN
(SELECT staffNo
FROM Staff
WHERE branchNo =
(SELECT branchNo
FROM Branch
WHERE street = ‘163 Main St’));

25
Pearson Education © 2009
Example : Nested subquery: use of IN

26
Pearson Education © 2009
ANY and ALL
 ANY and ALL may be used with subqueries that
produce a single column of numbers.
 With ALL, condition will only be true if it is
satisfied by all values produced by subquery.
 With ANY, condition will be true if it is satisfied
by any values produced by subquery.
 If subquery is empty, ALL returns true, ANY
returns false.
 SOME may be used in place of ANY.

27
Pearson Education © 2009
Example : Use of ANY
Find staff whose salary is larger than salary of at
least one member of staff at branch B003.
SELECT staffNo, fName, lName, position, salary
FROM Staff
WHERE salary > ANY
(SELECT salary
FROM Staff
WHERE branchNo = ‘B003’);

28
Pearson Education © 2009
Example : Use of ANY/SOME
 Inner query produces set {12000, 18000, 24000}
and outer query selects those staff whose salaries
are greater than any of the values in this set.

29
Pearson Education © 2009
Example : Use of ALL
Find staff whose salary is larger than salary of
every member of staff at branch B003.

SELECT staffNo, fName, lName, position, salary


FROM Staff
WHERE salary > ALL
(SELECT salary
FROM Staff
WHERE branchNo = ‘B003’);

30
Pearson Education © 2009
Example : Use of ALL

31
Pearson Education © 2009

You might also like