Relational Algebra - Queries
Relational Algebra - Queries
1.
σsubject = "database"(Books)
SELECT * FROM books WHERE subject = 'database';
2.
σsubject = "database" and price = "450"(Books)
SELECT * FROM books WHERE subject = 'database' AND price= 450;
3.
σ subject = "database" and price = "450" or year > "2010"(Books)
SELECT * FROM books WHERE (subject = 'database' AND price= 450) OR
publication_year > 2010;
(Q1) Find the names of sailors who have reserved boat 103.
SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid = R.sid and R.bid=103
-- or nested
SELECT S.sname FROM Sailors S WHERE S.sid IN(SELECT R.sid
FROM Reserves R WHERE R.bid = 103 )
(Q2) Find the names of sailors who have reserved a red boat.
(Q4) Find the names of sailors who have reserved at least one boat.
(Q5) Find the names of sailors who have reserved a red or a green boat.
-- or
SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid
AND R.bid = B.bid AND B.color = ‘red’
UNION
SELECT S2.sname FROM Sailors S2, Boats B2, Reserves R2
WHERE S2.sid = R2.sid AND R2.bid = B2.bid AND B2.color = ‘green’
(Q6) Find the names of sailors who have reserved a red and a green boat.
-- or
SELECT S.sname
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = ‘red’
INTERSECT
SELECT S2.sname
FROM Sailors S2, Boats B2, Reserves R2
WHERE S2.sid = R2.sid AND R2.bid = B2.bid AND B2.color = ‘green’
(Q7) Find the names of sailors who have reserved at least two boats.
SELECT S.sname
FROM Sailors S, Reserves R, Sailors S2, Reserves R2
WHERE S.sid = R.sid
and S2.sid = R2.sid
and S.sid = S2.sid
and R.bid != R2.bid
10. Find the names of sailors who have reserved all boats called Interlake.