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

Tutorial 5: Advanced SQL

The document describes a tutorial on advanced SQL concepts using example queries on tables for sailors, boats, and reservations. The queries demonstrate concepts like aggregation, joins, subqueries, and correlation. Some queries find the average age of sailors with a rating of 10, count distinct sailor names, or return the youngest sailor age for each rating level. Other queries return results for sailors who have reserved specific boats or combinations of boats.

Uploaded by

mars chou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Tutorial 5: Advanced SQL

The document describes a tutorial on advanced SQL concepts using example queries on tables for sailors, boats, and reservations. The queries demonstrate concepts like aggregation, joins, subqueries, and correlation. Some queries find the average age of sailors with a rating of 10, count distinct sailor names, or return the youngest sailor age for each rating level. Other queries return results for sailors who have reserved specific boats or combinations of boats.

Uploaded by

mars chou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Tutorial 5: Advanced SQL

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


Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)

Q1: Find the average age of all sailors with a rating of 10.

SELECT AVG (age)


FROM Sailors
WHERE rating=10
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)

Q2: Count the number of different sailors names.

SELECT COUNT (DISTINCT sname)


FROM Sailors
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)

Q3: Find the age of the youngest sailor for each rating level.

SELECT rating, MIN (age)


FROM Sailors
GROUP BY rating
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)

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

SELECT B.bid, COUNT (*)


FROM Boats B, Reserves R
WHERE R.bid=B.bid AND B.color=‘red’
GROUP BY B.bid

 Grouping over a join of two relations.


 What do we get if we remove B.color=‘red’ from the WHERE
clause and add a HAVING clause with this condition? Illegal!
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)

Q5: Find sailors whose rating is greater than that of some sailor
called Tom. Use SOME.

SELECT sid
FROM Sailors
WHERE rating > SOME (SELECT rating
FROM Sailors
WHERE sname=‘Tom’)
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)

Q6: Find sailors whose rating is greater than every sailor called Tom. Use
ALL.

SELECT sid
FROM Sailors
WHERE rating > ALL (SELECT rating
FROM Sailors
WHERE sname=‘Tom’)
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)

Q7: Find the sailors with the lowest rating. Use ALL.

SELECT sid
FROM Sailors
WHERE rating <= ALL (SELECT rating
FROM Sailors)
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)

Q8: Find name and age of the youngest sailor(s).

SELECT sname, age


FROM Sailors
WHERE age = (SELECT MIN(age)
FROM Sailors)
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)

Q9: Find the name of sailors who are older than the oldest sailors with a
rating of 10.

SELECT sname
FROM Sailors
WHERE age > ( SELECT MAX (age)
FROM Sailors
WHERE rating = 10)
OR
SELECT sname
FROM Sailors
WHERE age > ALL ( SELECT age
FROM Sailors
WHERE rating = 10)
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)

Q10: Find names of sailors who’ve reserved boat #103. Use EXISTS.

SELECT sname
FROM Sailors S
WHERE EXISTS (SELECT *
FROM Reserves R
WHERE R.bid=103 AND S.sid=R.sid)
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)

Q11: Find names of sailors who’ve made at most one reservation for boat
#103. Use nested query.

SELECT sname
FROM Sailors S
WHERE 2 > (SELECT COUNT(*)
FROM Reserves R
WHERE R.bid=103 AND S.sid=R.sid)
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)

Q12: Find the sailors who have made reservations for all red boats.

SELECT DISTINCT sid


FROM Reserves R1
WHERE NOT EXISTS (
(SELECT bid
FROM Reserves
WHERE color = ‘red’)
EXCEPT
(SELECT bid
FROM Reserves R2
WHERE R1.sid = R2.sid))
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)

Q13: Find names of sailors who’ve reserved boat #103. Use IN.

SELECT sname
FROM Sailors
WHERE sid IN (SELECT sid
FROM Reserves
WHERE bid=103)
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)

Q14: Find names of sailors who’ve reserved a red boat. Use IN.

SELECT sname
FROM Sailors
WHERE sid IN (SELECT sid
FROM Reserves
WHERE bid IN (SELECT bid
FROM Boats
WHERE color=‘red’))
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)

Q15: Find names of sailors who’ve NOT reserved a red boat. Use NOT IN
and IN.

SELECT sname
FROM Sailors
WHERE sid NOT IN (SELECT sid
FROM Reserves
WHERE bid IN (SELECT bid
FROM Boats
WHERE color=‘red’))
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)

Q16: Find names of sailors who’ve reserved a boat that is NOT red. Use
NOT IN and IN.

SELECT sname
FROM Sailors
WHERE sid IN (SELECT sid
FROM Reserves
WHERE bid NOT IN (SELECT bid
FROM Boats
WHERE color=‘red’))
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)

Q17: Find names of sailors who’ve NOT reserved a boat that is NOT red.
(i.e., who have reserved only red boats or haven’t reserved any boat.) Use
NOT IN.

SELECT sname
FROM Sailors
WHERE sid NOT IN (SELECT sid
FROM Reserves
WHERE bid NOT IN (SELECT bid
FROM Boats
WHERE color=‘red’))
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)

Extra 1: Find the sid’s of sailors who’ve reserved at least two


different boats on the same day.

SELECT sid
FROM Reserves
GROUP BY sid, day
HAVING count(*) > 1
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)

Extra 2: Find the names of sailors who’ve reserved at least two


different boats on the same day.

SELECT sname
FROM Reserves R, Sailors S
WHERE R.sid = S.sid
GROUP BY R.sid, day, sname
HAVING count(*) > 1
Sailors S
sid sname rating age
22 dustin 7 45
31 lubber 8 55
58 rusty 10 35
40 justin 9 30

Reserves R Boats B
sid bid day bid bname color
22 104 02/28/07 101 Interlake blue
22 102 02/28/07 102 Interlake red
58 103 03/12/07 103 Clipper green
40 104 03/12/07 104 Marine red
40 103 03/13/08

You might also like