Chapter 5 SQL
Chapter 5 SQL
SQL
SQL
• case INSENSITIVE
• Basic Syntactical Form
SELECT *
FROM Sailors;
• Get sailor ids and sailor names from Sailors table (all rows)
SELECT S.sid, S.sname
FROM Sailors S;
SELECT [DISTINCT] <columns list| *>
FROM <relations list>
• Get unique values of sailor names and ratings from Sailors table
SELECT DISTINCT S.sname, S.rating
FROM Sailors S;
• WRONG SQL
SELECT S.sname DISTINCT S.rating
FROM Sailors S;
SELECT [DISTINCT] <columns list| *>
FROM <relations list>
• Get unique sailor names over 18 and rating less or equal 8 from
Sailors table
SELECT DISTINCT S.sname
FROM Sailors S
WHERE S.age>18 AND
S.rating<=8;
SELECT [DISTINCT] <columns list| *>
FROM <relations list>
• Get unique sailor names over 18 or rating less or equal 8 from Sailors
table
SELECT DISTINCT S.sname
FROM Sailors S
WHERE S.age>18 OR
S.rating<=8;
• Get unique sailor names and show their age next year.
SELECT DISTINCT S.sname AS Name, S.age + 1 AS age_next_year
FROM Sailors S;
• Get sailor names whose twice their age is 3 times their rating
SELECT S.sname
FROM Sailors S
WHERE 2*S.age= 3* S.rating;
SELECT [DISTINCT] <columns list| *>
FROM <relations list>
• Find ages of sailors whose name begin and end with B and at least 3
characters long.
SELECT S.age
FROM Sailors S
WHERE S.sname LIKE ‘B_%B’;
• Find ages of sailors whose name contain the letter a
SELECT S.sname
FROM Sailors S
WHERE S.sname LIKE ‘%a%’;
• Find ages of sailors whose name is 3 characters long
SELECT S.sname
FROM Sailors S
WHERE S.sname LIKE ‘_ _ _’;
SELECT [DISTINCT] <columns list| *>
FROM <relations list>
• OR
SELECT S.sname
FROM Sailors S JOIN Reserves R ON S.sid = R.sid
WHERE R.bid = 103;
SELECT [DISTINCT] <columns list| *>
FROM <relations list>
SELECT S.sname
FROM Sailors S
WHERE S.sid IN
(SELECT R.sid
FROM Reserves R
WHERE R.bid IN
(SELECT B.bid
FROM Boats B
WHERE B.color = ‘red’);
SELECT [DISTINCT] <columns list| *>
FROM <relations list>
• Find names of sailors who reserved a red but not green boat
SELECT DISTINCT S.sname
FROM Reserves R, Sailors S, Boats B
WHERE S.sid = R.sid AND
SELECT DISTINCT S.sname
R.bid = B.bid AND
FROM Reserves R, Sailors S, Boats B
B.color=‘red’
WHERE S.sid = R.sid AND
EXCEPT R.bid = B.bid AND
SELECT DISTINCT S.sname B.color=‘red’ AND
FROM Reserves R, Sailors S, Boats B S.sid NOT IN
WHERE S.sid = R.sid AND (SELECT S.sid
R.bid = B.bid AND FROM Reserves R, Sailors S, Boats
B.color=‘green’; B
WHERE S.sid = R.sid AND
R.bid = B.bid AND
B.color=‘green’ );
SELECT [DISTINCT] <columns list| *>
FROM <relations list>
• Find names of sailors whose rating is better than some sailor named
‘Randy’
SELECT DISTINCT S.sname
FROM Sailors S
WHERE S.rating> ANY
(SELECT S2.rating
FROM Sailors S2
WHERE S2.sname = ‘Randy’);
SELECT [DISTINCT] <columns list| *>
FROM <relations list>
• Find the average age of sailors in each rating group with at least two
sailors. Sort by rating descending.
SELECT AVG(S.age), S.rating
FROM Sailors S
GROUP BY S.rating
HAVING COUNT(*)>1
ORDER BY 2 DESC;
Find the average age of sailors who are of voting age (i.e., at
least 18years old) for each rating level that has at least two
sailors.
• Find the average age of sailors who are of voting age
(i.e., at least 18years old) for each rating level that has
at least two sailors.
• SELECT S.rating, AVG ( S.age ) AS avgage
FROM Sailors S
WHERE S. age >= 18
GROUP BY S.rating
HAVING 1 < ( SELECT COUNT (*)
FROM Sailors S2
WHERE S.rating = S2.rating )