0% found this document useful (0 votes)
47 views16 pages

Tutorial 4: Basic SQL

Here are the schemas and sample data for the sailors, boats, and reserves relations.

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)
47 views16 pages

Tutorial 4: Basic SQL

Here are the schemas and sample data for the sailors, boats, and reserves relations.

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/ 16

Tutorial 4: Basic SQL

 Answer: Consider the following schemas.


CUST(cust-id, name), and
WITHDRAW(cust-id, acc-id, date, amount)

 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.

 SELECT DISTINCT name


FROM CUST C, WITHDRAW W
WHERE C.cust-id = W.cust-id and W.amount > 1k

2
 Answer: Consider the following schemas.
CUST(cust-id, name), and
WITHDRAW(cust-id, acc-id, date, amount)

 Retrieve the acc-id of accounts except those with smallest withdraw


amount.

 SELECT DISTINCT T1.acc-id


FROM WITHDRAW T1, WITHDRAW T2
WHERE T1.amount > T2.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)

 Sometimes there may be a “shared” account, namely, an account


with multiple owners.

 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

SELECT S.sid SELECT sid


FROM Sailors S, Reserves R OR FROM Reserves
WHERE 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)

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)

Q4: Find the sid’s of sailors who’ve reserved a red boat.

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)

Q5: Find the names of sailors who’ve reserved a red boat.

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)

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


different boats on the same day.

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)

Q8: Find sid’s of sailors who’ve reserved a red or a green boat

SELECT sid (SELECT sid


FROM Boats B, Reserves R FROM Boats B, Reserves R
WHERE R.bid=B.bid WHERE R.bid=B.bid
AND (B.color=‘red’ OR B.color=‘green’) AND B.color=‘red’)
UNION
(SELECT sid
OR FROM Boats B, Reserves R
WHERE R.bid=B.bid
AND B.color=‘green’)
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

SELECT R1.sid (SELECT sid


FROM Boats B1, Reserves R1, FROM Boats B, Reserves R
Boats B2, Reserves R2 WHERE R.bid=B.bid AND B.color=‘red’)
WHERE (R1.bid=B1.bid AND INTERSECT
B1.color=‘red’) AND (SELECT sid
(R2.bid=B2.bid AND FROM Boats B, Reserves R
B2.color=‘green’) AND WHERE R.bid=B.bid AND B.color=‘green’)
R1.sid=R2.sid
OR
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 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

You might also like