SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND...

50
SQL (2)

Transcript of SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND...

Page 1: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

SQL (2)

Page 2: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the names of sailors who have reserved a red or a green boat.

UNION, INTERSECT, AND EXCEPT

Page 3: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the names of sailors who have reserved a red or a green boat.

Page 4: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the names of sailors who have reserved both a red and a green boat.

Page 5: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the names of sailors who have reserved both a red and a green boat.

Page 6: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the names of sailors who have reserved both a red and a green boat. – use INTERSECT

Page 7: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the names of sailors who have reserved a red or a green boat. – Use UNION

Page 8: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the sids of all sailors who have reserved red boats but not green boats

Page 9: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the sids of all sailors who have reserved red boats but not green boats

Page 10: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find all sids of all sailors who have a rating of 10 or have reserved boat 104

Page 11: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find all sids of all sailors who have a rating of 10 or have reserved boat 104

Page 12: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Nested Query(SubQuery)

• A nested query is a query that has another query embedded within it;

• The embedded query is called a subquery.• A subquery appears within the WHERE clause

of a query. • Subqueries can sometimes appear in the

FROM clause or the HAVING clause

Page 13: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the names of sailors who have reserved boat 103

Page 14: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the names of sailors who have reserved boat 103

Page 15: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the name of sailors who have reserved a red boat

Page 16: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the name of sailors who have reserved a red boat

Page 17: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the name of sailors who have not reserved a red boat

Page 18: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the name of sailors who have not reserved a red boat

Page 19: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Set-Comparison Operators

• op ANY and op ALL• op : <, <=, =, <>, >=, > • SOME : a synonym for ANY

Page 20: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the sailors whose rating is better than some sailor called Horatio

Page 21: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the sailors whose rating is better than some sailor called Horatio

Page 22: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

• Replace ANY with ALL

SELECT S.SidFROM Sailors SWHERE S.Rating > ALL (SELECT S2.Rating

FROM Sailors S2 WHERE S2.sname =

‘Horatio’)

Page 23: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the sailors with the highest rating

Page 24: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the sailors with the highest rating

Page 25: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the names of sailors who have reserved both a red and a green boat.

Page 26: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the names of sailors who have reserved both a red and a green boat.

Page 27: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.
Page 28: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

AGGREGATE OPERATORS

1. COUNT ([DISTINCT] A) : the number of (unique) values in the A column.

2. SUM ([DISTINCT] A) : the sum of all (unique) values in the A column.

3. AVG ([DISTINCT] A) : the average of all (unique) values in the A column.

4. MAX : the maximal value in the column A5. MIN : the minimal value in the column A

Page 29: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the average age of all sailors

Page 30: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the average age of all sailors

Page 31: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the average age of sailors with a rating of 10

Page 32: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the average age of sailors with a rating of 10

Page 33: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the name and age of the oldest sailor

Page 34: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the name and age of the oldest sailor

?

Page 35: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the name and age of the oldest sailor

Page 36: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Count the number of sailors

Page 37: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Count the number of sailors

Page 38: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Count the number of different sailor names

Page 39: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Count the number of different sailor names

Page 40: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the name of sailors who are older than the oldest sailor with the rating of 10

Page 41: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the name of sailors who are older than the oldest sailor with the rating of 10

Page 42: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

The Group By and Having Clauses

Find the age of youngest sailor for each rating level

Page 43: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the age of youngest sailor for each rating level

Where i = 1, 2, …, 10

Page 44: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the age of youngest sailor for each rating level

Page 45: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the age of the youngest sailor who is eligible to vote (at least 18 years old) for each rating level with at least two such sailors

Page 46: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the age of the youngest sailor who is eligible to vote (at least 18 years old) for each rating level with at least two such sailors

Page 47: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

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

Page 48: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

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

X

Page 49: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the average age of sailors for each rating level that has at least two sailors

Page 50: SQL (2). Find the names of sailors who have reserved a red or a green boat. UNION, INTERSECT, AND EXCEPT.

Find the average age of sailors for each rating level that has at least two sailors