RA-Datalog-Tutorial - Sol PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Exercises on Relational Algebra and Datalog

Part I
Go over these exercises before you attend your tutorial. For each exercise, try it first and then
look at the answer. Discuss with the TA’s any problems you may have.

Question

Consider the Sailors-Boats-Reserves DB described in the text.


s (sid, sname, rating, age)
b (bid, bname, color)
r (sid, bid, date)
Write each of the following queries in RA and in Datalog.

1. Find the colors of boats reserved by Albert.


2. Find all sailor id’s of sailors who have a rating of at least 8 or reserved boat 103.
3. Find the names of sailors who have not reserved a red boat.
4. Find the sailor id’s of sailors with age over 20 who have not reserved a red boat.
5. Find the names of sailors who have reserved at least two boats.
6. Find the names of sailors who have reserved all boats.
7. Find the names of sailors who have reserved all boats called BigBoat.
8. Find the sailor id’s of sailors whose rating is better than some sailor called Bob.
9. Find the sailor id’s of sailors whose rating is better than every sailor called Bob.
10. Find the sailor id’s of sailors with the highest rating.
11. Find the name and age of the oldest sailor.
12. Find the names of sailors who have reserved every boat reserved by those with a lower
rating.

Answers
1. [( ( )) ]

( ) ( ) ( ) ( )

2. ( ( )) [ ( )]

( ) ( )
( ) ( )

3. ([ ( ) ( ( ) )] )
( ) ( ) ( )

( ) ( ) ( )

4. ( ( )) – ( ( ) )

( ) ( ) ( )

( ) ( ) ( )

5. [( ( ( )) ].

( ) ( ) ( ) ( )

6. ([ ( ) ( )] )

( ) ( ) ( )
( ) ( ) ( ) ( )
( ) ( )

7. ([ ( ) ( ( ))] )

( ) ( ) ( )
( ) ( ) ( ) ( )
( ) ( )

8. ( [ ( ) ( )])

( ) ( ) ( )

9. ( )– ( [ ( ) ( )])

( ) ( ) ( )
( ) ( ) ( )
10. ( )– ( [ ( ) ])

( ) ( ) ( )
( ) ( ) ( )

11. ([ ( )– ( ( ( ) ))] )

( ) ( ) ( )
( ) ( ) ( )

[ ( ( ))]

( )

( )

[ ( ) ( )] ( )]

Note: Above, we used abbreviations for brevity: e.g., sALS abbreciates


sailorsAndLesserSailors.

( ) ( ) ( )

( ) ( ) ( ) ( ) ( )

( ) ( )

You might also like