0% found this document useful (0 votes)
33 views6 pages

Assingment - 1: Sailors (Sid, Sname, Rating, Age) Sid Must Be Between

1. The document describes tables created for sailors, boats, and reservations. Data is inserted into each table. 2. Queries are written to analyze the data across the tables and return specific results such as average age by rating, names of sailors who reserved certain boats, and sailors who have reserved all boats. 3. Seven queries are listed with their SQL code and results to address different analyses of the data in the tables.

Uploaded by

shivam kumar
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)
33 views6 pages

Assingment - 1: Sailors (Sid, Sname, Rating, Age) Sid Must Be Between

1. The document describes tables created for sailors, boats, and reservations. Data is inserted into each table. 2. Queries are written to analyze the data across the tables and return specific results such as average age by rating, names of sailors who reserved certain boats, and sailors who have reserved all boats. 3. Seven queries are listed with their SQL code and results to address different analyses of the data in the tables.

Uploaded by

shivam kumar
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/ 6

ASSINGMENT – 1

 Sailors(sid, sname, rating, age) sid must be between


100 to 10,000

SQL> create table sailors(sid number(5) primary key check (sid between 100 and 10000)

, sname varchar2(20), rating number(3), age number(3));

Table created.

SQL> insert into sailors values(100, 'Raj', 7, 18);

SQL> insert into sailors values(101, 'Jeet', 15, 12);

SQL> insert into sailors values(102, 'Apurva', 10, 20);

SQL> insert into sailors values(103, 'Nasir', 14, 42);

SQL> insert into sailors values(104, 'Neel', 7, 30);

SQL> insert into sailors values(105, 'Toukir', 10, 25);

SQL> insert into sailors values(106, 'Akash', 14, 12);

SQL> insert into sailors values(107, 'Arijit', 10, 19);

 Boats(bid bname colour) bid must be between 100 to


300 and colour must be in Red, Green, Yellow, Blue

SQL> create table boats(bid number(3) primary key check (bid between 100 and 300), bn

ame varchar2(10), colour varchar2(8) check (colour in ('Red', 'Green', 'Yellow', 'Blue')));

Table created.
SQL> insert into boats values(200, 'Boat1', 'Red');

SQL> insert into boats values(201, 'Boat2', 'Yellow');

SQL> insert into boats values(202, 'Boat3', 'Green');

SQL> insert into boats values(203, 'Boat4', 'Blue');

 Reserves(bid, sid, day)

SQL> create table reserves(sid number(5) references sailors(sid), bid number(3) refer

ences boats(bid), day varchar2(10));

Table created.

SQL> insert into reserves values(102, 203, 'Wednesday');

SQL> insert into reserves values(105, 203, 'Saturday');

SQL> insert into reserves values(107, 203, 'Friday');

SQL> insert into reserves values(107, 200, 'Sunday');

SQL> insert into reserves values(105, 200, 'Monday');

SQL> insert into reserves values(107, 201, 'Monday');

SQL> insert into reserves values(105, 201, 'Sunday');

SQL> insert into reserves values(107, 202, Wednesday);

SQL> insert into reserves values(105, 202, 'Tuesday');

SQL> insert into reserves values(100, 200, 'Tuesday');

SQL> insert into reserves values(100, 202, 'Sunday');

SQL> insert into reserves values(103, 200, 'Thrusday');

SQL> insert into reserves values(104, 200, 'Friday');


DISPLAY THE CONTENTS OF THE TABLES

SQL> select * from sailors;

SID SNAME RATING AGE


---------- -------------------- ---------- ----------
100 Raj 7 18
101 Jeet 15 12
102 Apurva 10 20
103 Nasir 14 42
104 Neel 7 30
105 Toukir 10 25
106 Akash 14 12
107 Arijit 10 19

8 rows selected.

SQL> select * from boats;

BID BNAME COLOUR


------- ---------- --------
200 Boat1 Red
201 Boat2 Yellow
202 Boat3 Green
203 Boat4 Blue

4 rows selected.

SQL> select * from reserves order by sid;

SID BID DAY


--------- --------- ----------
100 200 Tuesday
100 202 Sunday
102 203 Wednesday
103 200 Thrusday
104 200 Friday
105 200 Monday
105 203 Saturday
105 201 Sunday
105 202 Tuesday
107 201 Monday
107 200 Sunday
107 203 Friday
107 202 Wednesday
8

13 rows selected.

1. Find avg age of all sailors (who are atleast 18 years


old ) for each rating level

SQL> select rating, avg(age) from sailors where age>=18 group by rating;

RATING AVG(AGE)
---------- ----------
14 27
7 24
10 21.3333333

2. Find name of sailors whose rating is more than some


sailors called apurva
SQL> select sname from sailors where rating>(select rating from sailors where sname='

Apurva');

SNAME
--------------------
Jeet

Nasir
Akash

3. Find name of sailors who are older than the oldest


sailor with rating 10

SQL> select sname from sailors where age > (select max(age) from sailors where rating=10);

SNAME
--------------------
Nasir

Neel

4. Find name of sailors who reserved boat 203

SQL> select sname from sailors where sid in(select sid from reserves where bid=203);

SNAME
--------------------
Apurva

Toukir

Arijit

5. Find sids of all sailors who have reserved red boats


but not green boats

SQL> (select sid from reserves where bid in(select bid from boats where colour='Red'))

minus(select sid from reserves where bid in(select bid from boats where colour='Green'));

SID
----------
103

104

6. Find name of sailors who have reserved both red


and green boats

SQL> select sname from sailors where sid in((select sid from reserves where bid in(se

lect bid from boats where colour='Red')) intersect(select sid from reserves where bid

in(select bid from boats where colour='Green')));

SNAME
------------------
Raj

Toukir

Arijit

7. Select name of sailors who have reserves all boats

SQL> select sname from sailors where sid in(select sid from reserves group by sid ha

ing count(*)=(select count(*) from boats));

SNAME
-----------------
Arijit

Toukir

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy