190 likes | 345 Views
Tutorial 5 : Advanced SQL. Question: Consider the following schema where sailors can reserve boats. The primary keys are underlined. Sailor s ( sid: integer , sname: string , rating: integer , age: real ) Boats( bid: integer , bname: string , color: string )
E N D
Question: • Consider the following schema where sailors can reserve boats. The primary keys are underlined. Sailors(sid: integer, sname: string, rating: integer, age: real) Boats(bid: integer, bname: string, color: string) Reserves(sid: integer, bid: integer, day: date) • Write the following queries in SQL.
Q1:Find names of sailors who’ve reserved boat #103. Use IN. Sailors(sid: integer, sname: string, rating: integer, age: real) Boats(bid: integer, bname: string, color: string) Reserves(sid: integer, bid: integer, day: date)
Q2:Find names of sailors who’ve reserved a red boat. Use IN. 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 names of sailors who’ve NOT reserved a red boat. Use NOT IN and IN. Sailors(sid: integer, sname: string, rating: integer, age: real) Boats(bid: integer, bname: string, color: string) Reserves(sid: integer, bid: integer, day: date)
Q4:Find names of sailors who’ve reserved a boat that is NOT red. Use NOT IN and IN. 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 names of sailors who’ve NOT reserved a boat that is NOT red. (i.e., who have reserved only red boats.) Use NOT IN. 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 names of sailors who’ve reserved boat #103.Use EXISTS. 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 names of sailors who’ve made at most one reservation for boat #103. Use nested query. 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 sailors whose rating is greater than that of some sailor called Tom. Use SOME. 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 sailors whose rating is greater than every sailor called Tom.Use ALL. 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 the sailors with the highest rating.Use ALL. 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 the average age of all sailors with a rating of 10. Sailors(sid: integer, sname: string, rating: integer, age: real) Boats(bid: integer, bname: string, color: string) Reserves(sid: integer, bid: integer, day: date)
Q12:Count the number of different sailors names. 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 name and age of the oldest sailor(s). 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 the name of sailors who are older than the oldest sailors with a rating of 10. 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 the age of the youngest sailor for each rating level. Sailors(sid: integer, sname: string, rating: integer, age: real) Boats(bid: integer, bname: string, color: string) Reserves(sid: integer, bid: integer, day: date)
Q16:For each red boat, find the number of reservations for this boat. Sailors(sid: integer, sname: string, rating: integer, age: real) Boats(bid: integer, bname: string, color: string) Reserves(sid: integer, bid: integer, day: date)