You are on page 1of 2

Consider the following schema of a relational database:

Sailors (sid, sname, rating, age)


Reserves (sid, bid, day)
Boats (bid, bname, colour)
For each of the following queries write an expression for Relational Algebra OR
Relational Calculus.
1. Find the names of sailors who have reserved boat 103.
•Solution 1:
πsname(σbid = 103 (Reserves ∞ Sailors))

•Solution 2 (more efficient)


πsname((σbid = 103 Reserves) ∞ Sailors)
2. Find the names of sailors who have reserved a red boat.
πsname((σcolor = ‘red’ Boats) ∞ Reserves ∞ Sailors )

πsname(πsid ((πbidσcolor = ‘red’ Boats)∞ Reserves )∞ Sailors )


)
Boats) ∞ailors )
3. Find the colour of boats reserved by Biswarup.
πcolor((σsname = ‘Lubber’ Sailor)∞ Reserves ∞ Boats )
4. Find the names of sailors who have reserved at least one boat.
πsname(Sailor∞ Reserves)
5. Find the names of sailors who have reserved a red boat or a green boat.
πsname(σcolor=‘red’ or color = ‘green’ Boats ∞ Reserves ∞ Sailors)
6. Find the names of sailors who have reserved a red boat and a green boat.
πsname(σcolor=‘red’ Boats ∞ Reserves ∞ Sailors) ∩
πsname(σcolor = ‘green’ Boats ∞ Reserves ∞ Sailors)
7. Find the names of sailors with age over 20 who have not reserved a red boat.
πsid (σage>20 Sailors) – πsid ((σcolor=‘red’ Boats) ∞ Reserves ∞ Sailors)
8. Find the names of sailors who have reserved all boats.
πsname ((πsid,bid (Reserves) / πbid (Boats)) ∞ Sailors)
9. Find the names of sailors who have reserved all boats called “Interlake”

πsname ((πsid,bid (Reserves) / πbid (σ bname=‘Interlake’ Boats)) ∞ Sailors)


Write a query to find second highest salary of an employee.

Select max(salary) from employee where salary NOT IN


(Select max(salary) from employee)

SELECT MAX(SALARY) FROM EMPLOYEE WHERE SALARY NOT IN (SELECT MAX(SALARY)


FROM EMPLOYEE)

You might also like