Tutorial 4: Basic SQL
Tutorial 4: Basic SQL
Write an SQL query to retrieve all the names of the customers who
have withdrawn more than 1k dollars in a single withdrawal.
If a customer made several such withdrawals, her/his name should
be reported only once.
2
Answer: Consider the following schemas.
CUST(cust-id, name), and
WITHDRAW(cust-id, acc-id, date, amount)
Note: this query will also return the account with smallest withdraw
amount if the same account has been withdrawn for a larger
amount. We will fix this query later on.
3
Answer: Consider the following schemas.
CUST(cust-id, name), and
WITHDRAW(cust-id, acc-id, date, amount)
Write an SQL query to return the acc-id of all the shared accounts.
You may assume that all the owners of a shared account have
made withdrawals from the account.
SELECT T1.acc-id
FROM WITHDRAW T1, WITHDRAW T2
WHERE T1.cust-id <> T2.cust-id and T1.acc-id = T2.acc-id
4
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 names of sailors who have reserved boat number 103.
SELECT sname
FROM Sailors S, Reserves R
WHERE S.sid=R.sid AND R.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)
Q2: Find the sid’s of sailors who’ve reserved at least one boat
Q3: Find the names of sailors who’ve reserved at least one boat
SELECT sname
FROM Sailors S, Reserves R
WHERE S.sid=R.sid
7
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)
SELECT sid
FROM Reserves R, Boats B
WHERE R.bid=B.bid AND B.color=‘red’
8
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)
SELECT sname
FROM Sailors S, Reserves R, Boats B
WHERE S.sid=R.sid and R.bid=B.bid
and B.color=‘red’
9
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 the sid’s of sailors who’ve reserved at least two different
boats on the same day.
SELECT R1.sid
FROM Reserves R1, Reserves R2
WHERE R1.sid=R2.sid and R1.day=R2.day
and R1.bid<>R2.bid
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)
SELECT sname
FROM Sailors S, Reserves R1, Reserves R2
WHERE S.sid = R1.sid and R1.sid=R2.sid and
R1.day=R2.day and R1.bid<>R2.bid
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 sid’s of sailors who’ve reserved a red and a green boat
Q10: Find sid’s of sailors with age over 20 who have not
reserved a red boat
(SELECT sid
FROM Sailors
WHERE age > 20)
EXCEPT
(SELECT sid
FROM Boats B, Reserves R
WHERE R.bid=B.bid AND B.color=‘red’)
Reserves R1 Reserves R2
sid bid day sid bid day
22 104 02/28/07 22 104 02/28/07
22 102 02/28/07 22 102 02/28/07
58 103 03/12/07 58 103 03/12/07
Sailors
sid sname rating age Boats
22 dustin 7 45.0 bid bname color
101 Interlake blue
31 lubber 8 55.5 102 Interlake green
58 rusty 10 35.0 103 Clipper green
104 Marine red
Reserves R1 Boats B1
sid bid day bid bname color
22 104 02/28/07 101 Interlake blue
22 102 02/28/07 102 Interlake green
58 103 03/12/07 103 Clipper green
104 Marine red
Reserves R2
Boats B2
sid bid day
22 104 02/28/07 bid bname color
22 102 02/28/07 101 Interlake blue
102 Interlake green
58 103 03/12/07
103 Clipper green
104 Marine red