+ All Categories
Home > Documents > SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves...

SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves...

Date post: 31-Mar-2015
Category:
Upload: elijah-trivett
View: 412 times
Download: 5 times
Share this document with a friend
Popular Tags:
27
SQL: Queries
Transcript
Page 1: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

SQL: Queries

Page 2: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Relational Schema

Sailors (sid, sname, rating, age)

Boats (bid, bname, color)

Reserves (sid, bid, day)

Page 3: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q11

Find the sailors with a rating above 7.

SELECT S.sid, S.sname, S.rating, S.age

FROM Sailors S

WHERE S.rating > 7;

Page 4: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q16

Find the sids of sailors who have reserved a red boat.

SELECT R.sid

FROM Boats B, Reserves R

WHERE B.bid = R.bid AND B.color = ‘red’;

Page 5: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q3

Find the colors of boats reserved by Lubber.

SELECT B.color

FROM Sailors S, Reserves R, Boats B

WHERE S.sid = R.sid AND R.bid = B.bid

AND S.sname = ‘Lubber’;

Page 6: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q4

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

SELECT S.sname

FROM Sailors S, Reserves R

WHERE S.sid = R.sid;

Page 7: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q17

Compute increments for the ratings of persons who have sailed two different boats on the same day.

SELECT S.sname, S.rating + 1 AS rating

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;

Page 8: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q18

Find the ages of sailors whose name begins and ends with B and has at least three characters.

SELECT S.age

FROM Sailors S

WHERE S.sname LIKE ‘B_%B’;

Page 9: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q20

Find all sids of sailors who have a rating of 10 or have reserved boat 104.

SELECT S.sidFROM Sailors SWHERE S.rating = 10UNIONSELECT R.sidFROM Reserves RWHERE R.bid = 104

Page 10: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q21

Find the names of sailors who have not reserved a red boat.

SELECT S.snameFROM Sailors SWHERE S.sid NOT IN (SELECT R.sid

FROM Reserves R WHERE R.bid IN (SELECT B.bid

FROM Boats B WHERE B.color = ‘red’));

Page 11: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q9

Find the names of sailors who have reserved all boat.

SELECT S.snameFROM Sailors SWHERE NOT EXISTS ((SELECT B.bid

FROM Boats B) EXCEPT (SELECT R.bid FROM Reserves R WHERE R.sid = S.sid));

Page 12: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q9 : Second Solution

Find the names of sailors who have reserved all boats.

SELECT S.snameFROM Sailors SWHERE NOT EXISTS ((SELECT B.bid

FROM Boats B)WHERE NOT EXISTS

(SELECT R.bid FROM Reserves R WHERE R.bid = B.bid AND R.sid = S.sid));

Page 13: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q28

Count the number of sailors.

SELECT COUNT (*)

FROM Sailors S;

Page 14: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q29

Count the number of different sailor names.

SELECT COUNT (DISTINCT S.sname)

FROM Sailors S;

Page 15: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q30

Find the names of sailors who are older than the oldest sailor with a rating of 10.

SELECT S.snameFROM Sailors SWHERE S.age > (SELECT MAX (S2.age)

FROM Sailors S2 WHERE S2.rating = 10);

Page 16: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q30 : Second Solution

Find the names of sailors who are older than the oldest sailor with a rating of 10.

SELECT S.sname

FROM Sailors S

WHERE S.age > ALL (SELECT S2.age)

FROM Sailors S2

WHERE S2.rating = 10);Note: error prone (“any” could be mistakenly used instead of “all”)

Page 17: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q33

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

SELECT B.bid, COUNT (*) AS sailorcount

FROM Boats B, Reserves R

WHERE R.bid = B.bid AND B.color = ‘red’

GROUP BY B.bid;

Page 18: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q33: Second Solution

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

SELECT B.bid, COUNT (*) AS sailorcount

FROM Boats B, Reserves R

WHERE R.bid = B.bid

GROUP BY B.bid

HAVING B.color = ‘red’;Note: This is incorrect because only columns that appears in the GROUP BY clause can appear in the HAVING clause, unless they appear as arguments to an aggregate operator in the HAVING clause.

Page 19: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q34

Find the average age of sailors for each rating level that has at least two sailors.

SELECT S.rating, AVG (S.age) AS averageAge

FROM Sailors S

GROUP BY S.rating

HAVING COUNT (*) >1;

Page 20: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q34: Second Solution

Find the average age of sailors for each rating level that has at least two sailors.

SELECT S.rating, AVG (S.age) AS averageAgeFROM Sailors SGROUP BY S.ratingHAVING 1 < (SELECT COUNT (*)

FROM Sailors S2WHERE S.rating = S2.rating);

Page 21: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q35

Find the average age of sailors who are of voting age (i.e. at least 18 years old) for each rating level that has at least two sailors.

SELECT S.rating, AVG (S.age) AS averageAgeFROM Sailors SWHERE S.age >= 18GROUP BY S.ratingHAVING 1 < (SELECT COUNT (*) FROM Sailors S2

WHERE S.rating = S2.rating);

Page 22: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q35: Second Solution?

Find the average age of sailors who are of voting age (i.e. at least 18 years old) for each rating level that has at least two sailors.

SELECT S.rating, AVG (S.age) AS averageAgeFROM Sailors SWHERE S.age >= 18GROUP BY S.ratingHAVING COUNT (*) > 1;

INCORRECT

Page 23: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q36

Find the average age of sailors who are of voting age (i.e. at least 18 years old) for each rating level that has at least two such sailors.

SELECT S.rating, AVG (S.age) AS averageAgeFROM Sailors SWHERE S.age >= 18GROUP BY S.ratingHAVING COUNT (*) > 1;

Page 24: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q36: Second Solution

Find the average age of sailors who are of voting age (i.e. at least 18 years old) for each rating level that has at least two such sailors.

SELECT S.rating, AVG (S.age) AS averageAgeFROM Sailors SWHERE S.age >= 18GROUP BY S.ratingHAVING 1 < (SELECT COUNT (*)

FROM Sailors S2 WHERE S.rating = S2.rating AND S2.age >= 18);

Page 25: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q36: Third Solution

Find the average age of sailors who are of voting age (i.e. at least 18 years old) for each rating level that has at least two such sailors.

SELECT Temp.rating, Temp.averageAgeFROM (SELECT S.rating, AVG (S.age) AS averageAge,

COUNT (*) AS ratingcount FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating) AS Temp)

WHERE Temp.ratingcount > 1;

Page 26: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q37

Find those ratings for which the average age of sailors is the minimum over all ratings.

SELECT Temp.rating, Temp.averageAge

FROM (SELECT S.rating, AVG (S.age) AS averageAge,

FROM Sailors S

GROUP BY S.rating) AS Temp)

WHERE Temp.averageAge = (SELECT MIN (Temp.averageAge)

FROM TEMP);

Page 27: SQL: Queries. Relational Schema Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)

Q37: Second Solution?

Find those ratings for which the average age of sailors is the minimum over all ratings.

SELECT Temp.rating, MIN (Temp.averageAge)

FROM (SELECT S.rating, AVG (S.age) AS averageAge,

FROM Sailors S

GROUP BY S.rating) AS Temp)

GROUP BY Temp.rating;


Recommended