Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL (2).

Similar presentations


Presentation on theme: "SQL (2)."— Presentation transcript:

1 SQL (2)

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

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

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

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

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

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

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

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

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

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

12 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

13 Find the names of sailors who have reserved boat 103

14 Find the names of sailors who have reserved boat 103

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

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

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

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

19 Set-Comparison Operators
op ANY and op ALL op : <, <=, =, <>, >=, > SOME : a synonym for ANY

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

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

22 Replace ANY with ALL SELECT S.Sid FROM Sailors S
WHERE S.Rating > ALL (SELECT S2.Rating FROM Sailors S2 WHERE S2.sname = ‘Horatio’)

23 Find the sailors with the highest rating

24 Find the sailors with the highest rating

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

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

27

28 AGGREGATE OPERATORS COUNT ([DISTINCT] A) : the number of (unique) values in the A column. SUM ([DISTINCT] A) : the sum of all (unique) values in the A column. AVG ([DISTINCT] A) : the average of all (unique) values in the A column. MAX : the maximal value in the column A MIN : the minimal value in the column A

29 Find the average age of all sailors

30 Find the average age of all sailors

31 Find the average age of sailors with a rating of 10

32 Find the average age of sailors with a rating of 10

33 Find the name and age of the oldest sailor

34 Find the name and age of the oldest sailor
?

35 Find the name and age of the oldest sailor

36 Count the number of sailors

37 Count the number of sailors

38 Count the number of different sailor names

39 Count the number of different sailor names

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

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

42 The Group By and Having Clauses
Find the age of youngest sailor for each rating level

43 Find the age of youngest sailor for each rating level
Where i = 1, 2, …, 10

44 Find the age of youngest sailor for each rating level

45 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

46 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

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

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

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

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


Download ppt "SQL (2)."

Similar presentations


Ads by Google