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

SQL Assignment 2

The document describes an SQL assignment to create tables, insert data, and write queries on a database with tables for sailors, boats, and boat reservations. It includes: 1) Instructions to create tables for sailors, boats, and reservations with attributes and constraints. 2) Sample data to insert into the tables. 3) 20 queries to write on the tables including finding names of sailors by criteria, averages by ratings, and counts of reservations. 4) Notes on SQL joins used to solve many queries.

Uploaded by

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

SQL Assignment 2

The document describes an SQL assignment to create tables, insert data, and write queries on a database with tables for sailors, boats, and boat reservations. It includes: 1) Instructions to create tables for sailors, boats, and reservations with attributes and constraints. 2) Sample data to insert into the tables. 3) 20 queries to write on the tables including finding names of sailors by criteria, averages by ratings, and counts of reservations. 4) Notes on SQL joins used to solve many queries.

Uploaded by

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

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: integer, sname: string, rating: integer, age: real)


Boats (bid: integer, bname: string, colour: string)
Reserves(sid: integer, bid: integer, day: date) date format "mm/dd/yyyy"

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

101 Interlake blue


102 Interlake red
103 Clipper green
104 Marine red

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

Create Table Sailors(


sid integer not null,sname varchar(20),rating integer, age decimal(3,1),
constraint PK_SID_Sailors primary key(sid))
Creating Table Boats

create table Boats(


bid integer not null,bname varchar(20), colour varchar(10),
constraint PK_BID_Boats primary key(bid));
Creating Table Reserves

Create table Reserves(


sid integer not null, bid integer not null,d_ay date not null,
Constraint PK_SID_BID_DAY_Reserves primary key (sid,bid,d_ay),
constraint FK_SID foreign key(sid) references Sailors(sid),
constraint FK_BID foreign key(bid) references Boats(bid));
Inserting Values into Boats Table

insert into Boats values (101,"Interlake","blue");


insert into Boats values (102,"Interlake","red");
insert into Boats values (103,"Clipper","green");
insert into Boats values (104,"Marine","red");
Inserting Values into Sailor Table

insert into sailors values (22,"Kapila",7,45.0);


insert into sailors values (29,"Mahesh",1,33.0);
insert into sailors values (31,"Shiromi",8,55.5);
insert into sailors values (32,"Gihan",8,25.5);
insert into sailors values (58,"Mahen",10,35.0);
insert into sailors values (64,"Silva",7,35.0);
insert into sailors values (71,"Lalith",10,16.0);
insert into sailors values (74,"Dilshan",9,35.0);
insert into sailors values (85,"Nanda",3,25.5);
insert into sailors values (95,"Kalani",3,63.5);
Inserting Values into Reserves Table
insert into Reserves values (22,101,'2013/10/10');
insert into Reserves values (22,102,'2013/10/10');
insert into Reserves values (22,103,'2013/08/10');
insert into Reserves values (22,104,'2013/07/10');
insert into Reserves values (31,102,'2013/10/11');
insert into Reserves values (31,103,'2013/06/11');
insert into Reserves values (31,104,'2013/12/11');
insert into Reserves values (64,101,'2013/05/09');
insert into Reserves values (64,102,'2013/08/09');
insert into Reserves values (74,103,'2013/08/09');
Q1. Select the names and ages of all sailors.

select sname, age from sailors


Q2. Find the names of sailors who have reserved at least one boat.

Select S.sname, R.bid,count(R.bid) as No_of_Boats from Sailors S inner join


Reserves R on R.sid = S.sid group by S.sname having count(R.bid) > 0
Q3. Find all sailors with a rating above 7

Select * from Sailors S where S.rating >7;


Q4. Find the names of sailors who have reserved boat number 103.

select S.sname,S.sid,B.bid from Sailors S inner join Reserves R on R.sid = S.sid


inner join Boats B on B.bid = R.bid where B.bid = 103
Q5. Find the olours of oats reserved y “ilva

select B.colour,S.sname,R.bid from Boats B inner join Reserves R on R.bid =


B.bid inner join Sailors S on S.sid = R.sid where S.sname = "Silva"
Q6. Compute increments (by 1) for the ratings of sailors who have sailed
two different boats on the same day.

Select S.sname,S.rating as Initial_Rating, S.rating +1 as


Rating_AfterIncrement from Sailors S, Reserves Rx, Reserves Ry where S.sid =
Rx.sid and S.sid = Ry.sid and Rx.d_ay = Ry.d_ay and Rx.bid <> Ry.bid group by
S.sname
Q7. Find the ages of sailors whose name begins and ends with B and has at
least three characters.

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,S.sid,B.bid from Sailors S inner join Reserves R on R.sid = S.sid


inner join Boats B on B.bid = R.bid where B.bid = 103
Q13. Find the names of sailors who have not 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

Select avg(age) as Average from sailors where rating = 10


Q17. For each red boat find the number of reservations for this boat.

select B.bid ,count(*) as no_of_reservations from Sailors S inner join Reserves


Ro R.sid = S.sid i er joi Boats B o B. id = R. id here B. olour = 'red‘
group by B.bid
Q18. Find the names of sailors who are older than the oldest sailor 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.

Select S.rating, count(*) as no_of_count,avg(S.age) as Average from Sailors S


group by S.rating having COUNT(*)>1
Q20. Age of sailors who are above 30 years if that rating level has at least
two such sailors.

select S.rating, count(*) as no_of_count,avg(S.age) as Average from Sailors S


where S.age>=30 group by S.rating having count(*)>1
Used below SQL Joins Techniques to find most of
the answers
End of Assignment 2

You might also like