Question

In: Computer Science

INTRO TO DATABASE Consider the Sailors-Boats-Reserves database described below. Sailors(sid: integer, sname: string, rating: integer, age:...

INTRO TO DATABASE

Consider the Sailors-Boats-Reserves database described below.

Sailors(sid: integer, sname: string, rating: integer, age: real)

Boats(bid: integer, bname: string, color: string)

Reserves(sid: integer, bid: integer, day: date)

Write each of the following queries in SQL.

1. Find the names and ages of sailors who have reserved at least two boats.

2. For each boat reserved by at least 2 distinct sailors, find the boat id and the average age of sailors who reserved it.

Solutions

Expert Solution

Answers:

1. Find the names and ages of sailors who have reserved at least two boats.

select sname,age from Sailors where sid in (select sid from Reserves  group by sid  having count(*)>=2);

2. For each boat reserved by atleast 2 distinct sailors, fine the boat id and the average age of sailors who reserved it.

select r.bid, avg(s.age) from Sailors s, reserves r
where s.sid = r.sid group by r.bid having count(r.sid)>=2;

Summary:

Below are some create table statement to your requirement and insert queries which i used to test above questions.

You can use them if you have not created tables and data in your database. Note these tables are created in oracle db. This might give error if you are using some other db.

But the queries in answer works for all db clients. You can use them in any db.

create table Sailors
(sid number primary key, sname varchar(50), rating number, age number);

create table Boats(
bid number primary key, bname varchar(50),  color varchar(50));


create table Reserves(sid number, bid  number, day date);

insert into Sailors values (1,'Bhaskar',5,26);
insert into Sailors values (2,'Guru',5,26);
insert into Sailors values (3,'Hema',5,27);
insert into Sailors values (4,'Maruti',5,30);
insert into Sailors values (5,'Faruk',5,25);
insert into Sailors values (6,'Sachin',5,26);

insert into Boats values (1,'abc','red');
insert into Boats values (2,'jhh','grey');
insert into Boats values (3,'opo','green');
insert into Boats values (4,'vovo','pink');
insert into Boats values (5,'momo','black');



insert into Reserves values (1,2,to_date('24-09-2020','dd-mm-yyyy'));
insert into Reserves values (3,2,to_date('22-09-2020','dd-mm-yyyy'));
insert into Reserves values (5,4,to_date('13-09-2020','dd-mm-yyyy'));
insert into Reserves values (4,4,to_date('22-09-2020','dd-mm-yyyy'));
insert into Reserves values (2,3,to_date('16-09-2020','dd-mm-yyyy'));
insert into Reserves values (2,5,to_date('18-09-2020','dd-mm-yyyy'));
insert into Reserves values (1,5,to_date('01-09-2020','dd-mm-yyyy'));
insert into Reserves values (5,3,to_date('09-09-2020','dd-mm-yyyy'));
insert into Reserves values (6,2,to_date('23-09-2020','dd-mm-yyyy'));

Output for answer 1:

Output for answer 2:

I know there is lot of information included in this answer, this is all just for your understanding. You may take the point 1 and 2 in Answer section for your requirement.

Hope this will help you. Enjoy!!!


Related Solutions

INTRO TO DATABASES Consider the boat reservation system: Sailors (sid, sname,rating, age) Boats(bid, bname, color) Reserves...
INTRO TO DATABASES Consider the boat reservation system: Sailors (sid, sname,rating, age) Boats(bid, bname, color) Reserves (sid, bid, day) Formulate the following question in relational algebra using two different sequences of relational operators: A. Find the colors of boats reserved by Lubber. B. Find the sids of sailors older than 20 who have not reserved a red boat.
INRO TO DATABASES Consider the following Schema: Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname:...
INRO TO DATABASES Consider the following Schema: Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname: string, color: string) Catalog(sid: integer, pid: integer, cost: real) The Catalog relation lists the prices charged for parts by Suppliers. Write the following queries in SQL using join, nested queries and set operators. 1. Find names of suppliers who supply every red or green part. 2. Find the sids of suppliers who supply every red part or supply every green part. 3. Find sids...
Question (3) Consider the following relations: Student(snum: integer, sname: string, major: string, level: string, age: integer)...
Question (3) Consider the following relations: Student(snum: integer, sname: string, major: string, level: string, age: integer) Class(name: string, meets at: string, room: string, fid: integer) Enrolled(snum: integer, cname: string) Faculty(fid: integer, fname: string, deptid: integer) The meaning of these relations is straightforward; for example, Enrolled has one record per student-class pair such that the student is enrolled in the class. Write the following queries in Oracle SQL. No duplicates should be printed in any of the answers. i) (2 points)...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname,...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname, major-dept, year) Courses (cid, cname, dept, credithours) Enrollment (sem-year, sid, cid, grade) Teaches (pid, cid, sem-year, class-size) where, Professors: All professors have professor id (pid), name (pname), department that they work (dept), and a phone number extension for their office (ext). Students: All students have id (sid), name (sname), department for their major (major-dept), and a year (year i.e, freshman, sophomore, junior, etc). Courses:...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname,...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname, major-dept, year) Courses (cid, cname, dept, credithours)Enrollment (sem-year, sid, cid, grade) Teaches (pid, cid, sem-year, class-size), Professors: All professors have professor id (pid), name (pname), department that they work (dept), and a phone number extension for their office (ext). Students: All students have id (sid), name (sname), department for their major (major-dept), and a year (yeari.e, freshman, sophomore, junior, etc). Courses: All courses have...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname,...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname, major-dept, year) Courses (cid, cname, dept, credithours) Enrollment (sem-year, sid, cid, grade) Teaches (pid, cid, sem-year, class-size) where, Professors: All professors have professor id (pid), name (pname), department that they work (dept), and a phone number extension for their office (ext). Students: All students have id (sid), name (sname), department for their major (major-dept), and a year (year i.e, freshman, sophomore, junior, etc). Courses:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT