0% found this document useful (0 votes)
10 views

L16 Relational Model and Structured Query Language

The document contains 15 practice problems involving SQL queries on tables for sailors, boats they reserve, and reservation details. The problems include retrieving data from single and multiple tables using joins, aggregating results using functions like count, average, sorting and filtering rows. Set operations like union and except are used to find rows that match multiple conditions or exclude certain results.

Uploaded by

sarthaksainia12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

L16 Relational Model and Structured Query Language

The document contains 15 practice problems involving SQL queries on tables for sailors, boats they reserve, and reservation details. The problems include retrieving data from single and multiple tables using joins, aggregating results using functions like count, average, sorting and filtering rows. Set operations like union and except are used to find rows that match multiple conditions or exclude certain results.

Uploaded by

sarthaksainia12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Database Systems

and Web
(15B11CI312)
PRACTICE PROBLEMS
JOINS

Source:
Ramakrishnan, Gehrke, Database Management Systems, Mcgraw-Hill, 3rd Edition, Addison-Wesley,2006.
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)
1. Create the following Tables with all the constraints.
2. Insert values in the above created tables.
3. Find all information of sailors who have reserved boat number 101.
4. Find the names of sailors who have reserved a red boat, and list in the order of age.
5. Find the names of sailors who have reserved at least one boat.
6. Find the ids and names of sailors who have reserved two different boats on the same day.
7. Find the ids of sailors who have reserved a red boat or a green boat.
8. Count the number of different sailor names.
9. Calculate the average age of all sailors.
10. Find sid’s of all sailors who’ve reserved red boat but not green boat.
11. Find the average age of sailors for each rating level that has at least two sailors.
12. Find the names of the sailors who have reserved both a red and a yellow boat.
13. Find the sailor id’s of sailors whose rating is better than some sailor called Bob.
14. List the names of those sailors whose name has only five characters and third alphabet ends
with 's'.
15. Find the SID of all sailors who have reserved red boats but not green boats.
1. Create Tables
CREATE TABLE sailors ( sid integer not null,
sname varchar(32),
rating integer,
age real,
CONSTRAINT PK_sailors PRIMARY KEY (sid) );

CREATE TABLE reserves ( sid integer not null,


bid integer not null,
day datetime not null,
CONSTRAINT PK_reserves PRIMARY KEY (sid, bid, day),
FOREIGN KEY (sid) REFERENCES sailors(sid),
FOREIGN KEY (bid) REFERENCES boats(bid) );
2. Insert Data
INSERT INTO sailors ( sid, sname, rating, age ) VALUES ( 22, 'Dustin', 7, 45.0 );
INSERT INTO reserves ( sid, bid, day ) VALUES ( 22, 101, '1998-10-10’);

3. Find all information of sailors who have reserved boat number 101.
SELECT * FROM Sailors, Reserves
WHERE Sailors.sid = Reserves.sid AND Reserves.bid = 101;

4. Find the names of sailors who have reserved a red boat, and list in the
order of age.
SELECT S.sname, S.age FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = ‘red’
ORDER BY S.age;
5. Find the names of sailors who have reserved at least one boat.
SELECT sname FROM Sailors S, Reserves R
WHERE S.sid = R.sid;

6. Find the ids and names of sailors who have reserved two different boats
on the same day.
SELECT DISTINCT S.sid, S.sname
FROM Sailors S, Reserves R1, Reserves R2
WHERE S.sid = R1.sid AND S.sid = R2.sid
AND R1.day = R2.day AND R1.bid <> R2.bid
7. Find the ids of sailors who have reserved a red boat or a green boat.
SELECT R.sid
FROM Boats B, Reserves R
WHERE R.bid = B.bid AND B.color = ‘red’
UNION
SELECT R2.sid
FROM Boats B2, Reserves R2
WHERE R2.bid = B2.bid AND B2.color = ‘green’;
8. Count the number of different sailor names.
SELECT COUNT( DISTINCT S.sname )
FROM Sailors S;

9. Calculate the average age of all sailors.


SELECT AVG(s.age)
FROM Sailors S

10. Find sid’s of all sailors who’ve reserved red boat but not green boat.
SELECT R.sid
FROM Boats B, Reserves R
WHERE R.bid=B.bid AND B.color=‘red’
EXCEPT
SELECT R.sid
FROM Boats B, Reserves R
WHERE R.bid=B.bid AND B.color=‘green’;
11. Find the average age of sailors for each rating level that has at least two sailors.
SELECT S.rating, AVG(S.age) AS avg_age
FROM Sailors S
GROUP BY S.rating
HAVING COUNT(*) > 1;

12. Find the names of the sailors who have reserved both a red and a yellow boat.
select s.sname
from sailors s, boats b, reserves r
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 = 'yellow';
13. Find the sailor id’s of sailors whose rating is better than some sailor
called Bob.
SELECT s1.sid FROM sailors s1, sailors s2
WHERE s1.rating>s2.rating AND s2.sname=`Bob’ ;

14. List the names of those sailors whose name has only five characters
and third alphabet ends with 's’.
SELECT sname
FROM sailors
WHERE length(sname) = 5 and sname like ’__s%’;
//There are two dashes without space.
15. Find the SID of all sailors who have reserved red boats but not
green boats.
SELECT R1.SID FROM BOATS B1, RESERVES R1
WHERE R1.BID = B1.BID AND B1.COLOR = 'RED’
MINUS
SELECT R2.SID FROM BOATS B2, RESERVES R2
WHERE R2.BID = B2.BID ANDB2.COLOR = ‘GREEN’;

You might also like