RA and SQL Exercises
1
Exercise 1-
Get the Left Outer Join
Name Dept Name Addr
R Jack Physics Jack Irvine
S
Mike LA
Tom ICS Mary Riverside
What is the result of R left outer join S?
R.name R.Dept S.name S.addr
Jack Physics Jack Irvine
RS= Jack
Jack
Physics
Physics
Mike
Mary
LA
Riverside
Tom ICS Jack Irvine
Tom ICS Mike LA
Tom ICS Mary Riverside
Name Dept Addr
Left outer join Jack Physics Irvine
R S= Tom ICS NULL
Pad null value for left dangling tuples.
2
Exercise 2-
Get the Right Outer Join
Name Dept Name Addr
Jack Irvine
R Jack Physics S
Mike LA
Tom ICS Mary Riverside
What is the result of R right outer join S?
R.name R.Dept S.name S.addr
Jack Physics Jack Irvine
Jack Physics Mike LA
RS Jack Physics Mary Riverside
Tom ICS Jack Irvine
Tom ICS Mike LA
Tom ICS Mary Riverside
Name Dept Addr
Right outer join Jack Physics Physics
Mike NULL LA
R S Mary NULL Riverside
Pad null value for right dangling tuples.
3
Exercise 3-
Get the Full Outer Join
Name Addr
Name Dept Jack Irvine
R Jack Physics Mike LA
S
Tom ICS Mary Riverside
What is the result of R full outer join S?
R.name R.Dept S.name S.addr
Jack Physics Jack Irvine
RS Jack
Jack
Physics
Physics
Mike
Mary
LA
Riverside
Tom ICS Jack Irvine
Tom ICS Mike LA
Tom ICS Mary Riverside
Name Dept Addr
Jack Physics Physics
Full outer join Tom ICS NULL
R S Mike NULL LA
Mary NULL Riverside
Pad null values for both left and right dangling tuples. 4
Exercise 4
Given the following relational schema, list all
customers and the number of orders each
customer has placed (Remember to also
include customers who have not yet placed
an order!)
Customer(customer_id, customer_name)
Order(order_id, customer_id, product_id,
quantity)
5
Solution 4
List all customers and the number of orders
each customer has placed (Remember to also
include customers who have not yet placed
an order!)
Select Customer.customer_name,
COUNT(Order.quantity) as number_of_orders
From Customer Left Outer Join Orders On
Customer.customer_id = Order.customer_id
Group by Customer.customer_name
6
Exercise 5
Consider a relation R(A,B,C), express
the FD AB-> C in relational algebra
(A ,B ,C )(R)
R1 1 1 1
A=A1 and B=B1 and C<>C1(R x R1)=Ø
7
Exercise 6
Consider a relation R(A,B,C), express in
relational algebra that A is a key.
(A ,B ,C )(R)
R1 1 1 1
A=A1 and B<>B1 (R x R1)=Ø
A=A1 and C<>C1(R x R1)=Ø
8
Views
A View is a “virtual table”
= A relation defined in terms of
the contents of other tables
and views
Syntax:
CREATE VIEW name AS query;
Or, to rename attributes
CREATE VIEW name(A1, A2, ...,
Am) AS query
9
Example: View Definition
Create a view called CanDrink that contains the
drinker-beer pairs such that the drinker
frequents at least one bar that serves the beer.
Frequents(drinker, bar)
Sells(bar, beer)
CREATE VIEW CanDrink AS
SELECT drinker, beer
FROM Frequents, Sells
WHERE Frequents.bar = Sells.bar;
10
Example: Accessing a View
We could query a view as if it were a
base table
A base table is a relation whose value
is really stored in the database
Example: Find the beers that Sally
drinks.
SELECT beer FROM CanDrink
WHERE drinker = ’Sally’;
11
What Happens When a View
Is Used?
The DBMS starts by interpreting the
query as if the view were a base table.
Typically, DBMS turns the query into
something like relational algebra.
The definitions of any views used by
the query are also replaced by their
algebraic equivalents, and “spliced into”
the expression tree for the query.
12
Example: View Expansion
SELECT beer PROJbeer
FROM CanDrink
WHERE drinker = ’Sally’; SELECTdrinker=„Sally‟
CanDrink
CREATE VIEW CanDrink AS PROJdrinker, beer
SELECT drinker, beer
FROM Frequents, Sells JOIN
WHERE
Frequents.bar = Sells.bar; Frequents Sells
13
Exercise 7
Consider the following relational database
scheme:
Supplier(s#, sname, city)
Supplies(p#, s#, price)
Parts(p#, pname, weight)
Create a view best_suppliers with the
attributes (s#, sname, p#, pname, price) such
that s# offers the lowest price among all
suppliers who supply that p#
14
Solution 7
Create a view best_suppliers with the attributes (s#,
sname, p#, pname, price) such that s# offers the
lowest price among all suppliers who supply that p#
Create view best_suppliers (s#, sname, p#, pname, price) As
Select sr.s#, sr.sname, ss.p#, p.pname, ss.price
From Supplier sr, Supplies ss, Parts p
Where sr.s# = ss.s#
AND ss.p# = p.p#
AND ss.price = (Select min(price)
From Supplies
Group by p#
Having p# = ss.p#);
15
Exercise 8
Consider the following relational database
scheme:
Supplier(s#, sname, city)
Supplies(p#, s#, price)
Parts(p#, pname, weight)
Create a view HeavyCheapParts with the
attributes (p#, pname, price,weight) such that
p# has a price lower than $1000 and a weight
more than 200 KG.
16
Solution 8
Create view HeavyCheapParts(p#, pname,
price,weight) As
Select p.p#, p.pname, s.price, p.weight
From Part p, Supplies s
Where p.p# = s.p# AND s.price < 1000
AND p.weight >200
17
Exercise 9
Write a query that list all the suppliers
that supply some parts heavier than
500 kg and cheaper than $200.
(Use your answer in exercise 8)
Supplier(s#, sname, city)
Supplies(p#, s#, price)
Parts(p#, pname, weight)
HeavyCheapParts (p#, pname,
price,weight)
18
Solution 9
List all the suppliers that supply some parts
heavier than 500 kg and cheaper than $200
Select s.sname from
HeavyCheapParts h, Suppiers s, Supplies ss
Where ss.p#=h.p# AND ss.s#=s.s#
AND h.price < 200
AND h.weight > 500
19