Beginning SQL
Beginning SQL Beginning SQL
Chapter 3 Member Name The member is called Jack Johnson The member is called Seymour Botts The member is called Susie Simons The member is called Jamie Hills The member is called Stuart Dales The member is called William Doors The member is called Doris Night That completes the look at concatenation. The next section shows you how to select data from more than one table at a time. Selecting Data from More Than One Table 90 Using the SQL you’ve learned so far, you can extract data from only one table in the database, which is quite limiting because often answers require data from more than one table. The developers of SQL realized this limitation and implemented a way of joining data from more than one table into one results set. Using the word joining is no accident: in SQL the JOIN keyword joins one or more tables together in a results set. Chapter 8 examines all the different types of joins, but this chapter covers the most commonly used (and also the easiest to use) join: the inner join. To see why joins are necessary and useful, begin with a problem. Say that you want a list of all the film names, years of release, and ratings for the Historical film category. Assume that you know the category name but don’t know what the CategoryId value is for Historical. If SQL didn’t support joins, your first task would be to look in the Category table for the CategoryId for the category with a value Historical: SELECT CategoryId FROM Category WHERE Category = ‘Historical’; The preceding SQL returns just one result: 6. Now you know that the CategoryId for Historical is 6, and that can be used with the CategoryId column in the Films table to get a list of films in the Historical category: SELECT FilmName, YearReleased, Rating FROM Films WHERE CategoryId = 6; Running the preceding SQL returns the following results:
- Page 168: Chapter 3 The statement returns the
- Page 172: Chapter 3 NOT Operator Examples thu
- Page 176: Chapter 3 2. Using the following SQ
- Page 180: Chapter 3 When you execute the quer
- Page 184: Chapter 3 Now that you’re acquain
- Page 188: Chapter 3 It’s as simple as that!
- Page 192: Chapter 3 76 YearReleased 1947 1967
- Page 196: Chapter 3 78 FilmName Rating Gone w
- Page 200: Chapter 3 2. Add a WHERE clause to
- Page 204: Chapter 3 Executing this SQL provid
- Page 208: Chapter 3 84 FullName First name is
- Page 212: Chapter 3 86 FullName First name is
- Page 216: Chapter 3 Even though DateOfBirth i
- Page 222: FilmName YearReleased Rating Sense
- Page 226: Figure 3-1 The first task when tack
- Page 230: That’s all well and good, but so
- Page 234: Category FirstName LastName War Jen
- Page 238: Extracting Information FirstName La
- Page 242: If you take this further and join t
- Page 246: SELECT * FROM FavCategory Figure 3-
- Page 250: Category MemberId Thriller 5 Thrill
- Page 254: Remember to add brackets around the
- Page 258: surprisingly, the MemberDetails tab
- Page 262: LocationId 2 2 2 2 1 1 1 Figure 3-6
- Page 266: includes that record. You should al
Chapter 3<br />
Member Name<br />
The member is called Jack Johnson<br />
The member is called Seymour Botts<br />
The member is called Susie Simons<br />
The member is called Jamie Hills<br />
The member is called Stuart Dales<br />
The member is called William Doors<br />
The member is called Doris Night<br />
That completes the look at concatenation. The next section shows you how to select data from more than<br />
one table at a time.<br />
Selecting Data from More Than One Table<br />
90<br />
Using the <strong>SQL</strong> you’ve learned so far, you can extract data from only one table in the database, which is<br />
quite limiting because often answers require data from more than one table. The developers of <strong>SQL</strong> realized<br />
this limitation and implemented a way of joining data from more than one table into one results set.<br />
Using the word joining is no accident: in <strong>SQL</strong> the JOIN keyword joins one or more tables together in a<br />
results set. Chapter 8 examines all the different types of joins, but this chapter covers the most commonly<br />
used (and also the easiest to use) join: the inner join.<br />
To see why joins are necessary and useful, begin with a problem. Say that you want a list of all the film<br />
names, years of release, and ratings for the Historical film category. Assume that you know the category<br />
name but don’t know what the CategoryId value is for Historical.<br />
If <strong>SQL</strong> didn’t support joins, your first task would be to look in the Category table for the CategoryId for<br />
the category with a value Historical:<br />
SELECT CategoryId<br />
FROM Category<br />
WHERE Category = ‘Historical’;<br />
The preceding <strong>SQL</strong> returns just one result: 6. Now you know that the CategoryId for Historical is 6,<br />
and that can be used with the CategoryId column in the Films table to get a list of films in the<br />
Historical category:<br />
SELECT FilmName, YearReleased, Rating<br />
FROM Films<br />
WHERE CategoryId = 6;<br />
Running the preceding <strong>SQL</strong> returns the following results: