Tutorial 11 Part1 More On RA SQL
Tutorial 11 Part1 More On RA SQL
1
Exercise 1-
Get the Left Outer Join
Name Dept Name Addr
R Jack Physics Jack Irvine
S
Mike LA
Tom ICS Mary Riverside
RS= Jack
Jack
Physics
Physics
Mike
Mary
LA
Riverside
Tom ICS Jack Irvine
Tom ICS Mike LA
Tom ICS Mary Riverside
RS Jack
Jack
Physics
Physics
Mike
Mary
LA
Riverside
Tom ICS Jack Irvine
Tom ICS Mike LA
Tom ICS Mary 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
Sells(bar, beer)
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)
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
19