0% found this document useful (0 votes)
29 views5 pages

Relational Algebra - Queries

Algebra

Uploaded by

unknownusers157
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views5 pages

Relational Algebra - Queries

Algebra

Uploaded by

unknownusers157
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Select Operation (σ):

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;

Project Operation (Π):


1.
Πsubject, author (Books)
SELECT subject, author FROM Books;

Union Operation (U):


Π author(Books) 𝖴 Π author(Articles)
SELECT author FROM books
UNION
SELECT author FROM articles;

Intersection Operation (∩):


Π author(Books) ∩ Π author(Articles)
SELECT author FROM books

Dr. P. Sammulal, Professor of CSE, JNTUH


INTERSECT
SELECT author FROM articles;
Set Difference (−):
Π author (Books) − Π author (Articles)
SELECT author FROM books
EXCEPT
SELECT author FROM articles;

Cartesian Product (Χ):


rΧs
SELECT emp.eno, emp1.ename FROM emp, emp1

(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.

SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE B.color='red'


AND B.bid=R.bid AND S.sid = R.sid

Dr. P. Sammulal, Professor of CSE, JNTUH


-- or nested
SELECT S.sname FROM Sailors S WHERE S.sid IN ( SELECT R.sid
FROM Reserves R WHERE R.bid IN(SELECT B.bid
FROM Boats B WHERE B.color = ‘red’ )

(Q3) Find the colors of boats reserved by Lubber.

SELECT B.color FROM Sailors S, Reserves R, Boats B


WHERE S.sname='Lubber' AND S.sid=R.sid AND R.bid = B.bid

(Q4) Find the names of sailors who have reserved at least one boat.

SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid

(Q5) Find the names of sailors who have reserved a red or a green boat.

SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid


AND R.bid = B.bid AND (B.color = ‘red’ OR B.color = ‘green’)

-- 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.

Dr. P. Sammulal, Professor of CSE, JNTUH


SELECT S.sname
FROM Sailors S, Reserves R1, Boats B1, Reserves R2, Boats B2
WHERE S.sid = R1.sid
AND R1.bid = B1.bid
AND S.sid = R2.sid
AND R2.bid = B2.bid
AND B1.color=‘red’
AND B2.color = ‘green’

-- 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

Dr. P. Sammulal, Professor of CSE, JNTUH


(Q8) Find the sids of sailors with age over 20 who have not reserved a red boat.

SELECT S.sname FROM Sailors S, Reserves R, Boats B


WHERE B.color != 'red' and B.bid = R.bid and S.sid = R.sid and S.sid > 20

9. Find the names of sailors who have reserved all boats.

SELECT S.sailor_name FROM Sailors S WHERE NOT EXISTS (


SELECT *
FROM Boats B WHERE NOT EXISTS (SELECT * FROM Reserves R
WHERE R.sailor_id = S.sailor_id AND R.boat_id = B.boat_id
)
);

10. Find the names of sailors who have reserved all boats called Interlake.

SELECT S.sailor_name FROM Sailors S


JOIN Reserves R ON S.sailor_id = R.sailor_id
JOIN Boats B ON R.boat_id = B.boat_id
WHERE B.boat_name = 'Interlake'
GROUP BY S.sailor_id, S.sailor_name
HAVING COUNT(DISTINCT B.boat_id) = (SELECT COUNT(*)
FROM Boats WHERE boat_name = 'Interlake');

Dr. P. Sammulal, Professor of CSE, JNTUH

You might also like