SQL Assignment 2
SQL Assignment 2
DBMS
Deadline – 22nd May 2016
Batch No : MIT 2015/2016
MIT 1103 - Database Systems
Index No : 15550235
Using a DBMS (such as Oracle, MySQL) that you are able to access,
create the following tables with appropriate constraints and insert the data given below.
Sailors
sid sname rating age
22 Kapila 7 45.0
29 Mahesh 1 33.0
31 Shiromi 8 55.5
32 Gihan 8 25.5
58 Mahen 10 35.0
64 Silva 7 35.0
71 Lalith 10 16.0
74 Dilshan 9 35.0
85 Nanda 3 25.5
95 Kalani 3 63.5
Boats
bid bname colour
Reserves
sid bid date
22 101 10/10/2013
22 102 10/10/2013
22 103 10/08/2013
22 104 10/07/2013
31 102 11/10/2013
31 103 11/06/2013
31 104 11/12/2013
64 101 09/05/2013
64 102 09/08/2013
74 103 09/08/2013
Creating Table Sailors
select S.sname, S.age from Sailors S where S.sname like "B%B" and
char_length(S.sname) > 3
Q8. Find the names of sailors who have reserved a red or a green boat.
Select distinct S.sname from Sailors S inner join Reserves R on R.sid = S.sid
inner join Boats B on B.bid = R.bid where B.colour in ("red" ,"green")
Q9. Find the names of sailors who have reserved both a red and a green
boat.
select SS.sname from Sailors SS where exists (select distinct S.sname from Sailors S
inner join Reserves R on R.sid = S.sid inner join Boats B on B.bid = R.bid where B.colour
= 'red' AND S.sid = SS.sid) AND exists(select distinct S.sname from Sailors S inner join
Reserves R on R.sid = S.sid inner join Boats B on B.bid = R.bid where B.colour = 'green'
AND S.sid = ss.sid)
Q10. Find the names of sailors who have reserved red boats but not green
boats.
select SS.sname from Sailors SS where SS.sid in (select distinct S.sid from
Sailors S inner join Reserves R on R.sid = S.sid inner join Boats B on B.bid =
R.bid where B.colour = 'red' ) AND SS.sid not in (select distinct S.sid from
Sailors S inner join Reserves R on R.sid = S.sid inner join Boats B on B.bid =
R.bid where B.colour = 'green' )
Q11. Find sids of sailors who have a rating of 10 or reserved boat 104.
sele tS.sid fro Sailors S here S.rati g=' ‘ u io sele t R.sid fro Reser es
R where R.bid='104'
Q12. Find the names of sailors who have reserved boat number 103
Select S.sname from Sailors S where S.sid not in (select R.sid from Reserves R
where R.bid = 103);
Q14. Find the names of sailors who have reserved a red boat.
select S.sname from Sailors S inner join Reserves R on R.sid = S.sid inner join
Boats B o B. id = R. id here B. olour = 'red‘ group y S.s a e
Q15. Write the query in (Q12) using a correlated nested query
Select S.sname from Sailors S where exists (Select * from Reserves R where
R.bid = 103 and R.sid = S.sid);
Q16. Find the average age of sailors with a rating of 10
Selelect S.sname from Sailors S where S.age > (Select max(SS.age) from
Sailors SS where SS.rating = 10)
Q19. Find the average age of sailors for each rating level that rating level
has at least two sailors.