Beginning SQL
Beginning SQL Beginning SQL
the category list and a list of which members like each category. To get details of the members’ names, you need to join the results of the first join to the MemberDetails table, but again, you want the results from the previous join even if the MemberDetails table contains no match. Start simple and create a query that returns all the categories and a list of IDs of members who chose that category as a favorite: SELECT Category.Category, FavCategory.MemberId FROM FavCategory RIGHT OUTER JOIN Category ON FavCategory.CategoryId = Category.CategoryId ORDER BY Category; The Category table is on the right of the join, and as it’s the table whose rows you want regardless of a match in FavCategory, the Category table requires a RIGHT OUTER JOIN. Executing the query so far produces the following results: Category MemberId Comedy 4 Comedy 6 Comedy 12 Film Noir NULL Historical 10 Historical 12 Historical 14 Horror 10 Horror 11 Horror 13 Romance 1 Romance 14 Sci-fi 4 Sci-fi 11 Sci-fi 13 Thriller 5 Thriller 10 Thriller 12 War 1 War 6 Selecting Data from Different Tables Table continued on following page 223
- Page 434: For example, the following code add
- Page 438: Running the query gives the answer
- Page 442: SELECT MAX(LastName), MIN(LastName)
- Page 446: immediately after the GROUP BY stat
- Page 450: Executing the final SQL provides th
- Page 456: Chapter 7 The INNER JOIN links the
- Page 460: Chapter 7 FilmName FirstName LastNa
- Page 464: Chapter 7 212 As mentioned, that wo
- Page 468: Chapter 7 Category Street Romance T
- Page 472: Chapter 7 216 MD1.First MD1.Last MD
- Page 476: Chapter 7 SELECT MD1.MemberId, MD1.
- Page 480: Chapter 7 220 SELECT Category, Film
- Page 484: Chapter 7 222 Location.Street Membe
- Page 490: Category FirstName LastName Horror
- Page 494: You can join the results from two o
- Page 498: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
- Page 502: function and the query still works,
- Page 506: Summary Selecting Data from Differe
- Page 512: Chapter 8 A WHERE clause is added t
- Page 516: Chapter 8 238 FilmName PlotSummary
- Page 520: Chapter 8 If you think that your jo
- Page 524: Chapter 8 In the query, the Categor
- Page 528: Chapter 8 244 There is one area in
- Page 532: Chapter 8 246 SELECT FirstName, Las
the category list and a list of which members like each category. To get details of the members’ names,<br />
you need to join the results of the first join to the MemberDetails table, but again, you want the results<br />
from the previous join even if the MemberDetails table contains no match.<br />
Start simple and create a query that returns all the categories and a list of IDs of members who chose<br />
that category as a favorite:<br />
SELECT Category.Category, FavCategory.MemberId<br />
FROM FavCategory<br />
RIGHT OUTER JOIN Category ON FavCategory.CategoryId = Category.CategoryId<br />
ORDER BY Category;<br />
The Category table is on the right of the join, and as it’s the table whose rows you want regardless of a<br />
match in FavCategory, the Category table requires a RIGHT OUTER JOIN. Executing the query so far<br />
produces the following results:<br />
Category MemberId<br />
Comedy 4<br />
Comedy 6<br />
Comedy 12<br />
Film Noir NULL<br />
Historical 10<br />
Historical 12<br />
Historical 14<br />
Horror 10<br />
Horror 11<br />
Horror 13<br />
Romance 1<br />
Romance 14<br />
Sci-fi 4<br />
Sci-fi 11<br />
Sci-fi 13<br />
Thriller 5<br />
Thriller 10<br />
Thriller 12<br />
War 1<br />
War 6<br />
Selecting Data from Different Tables<br />
Table continued on following page<br />
223