Beginning SQL

Beginning SQL Beginning SQL

marjan.fesb.hr
from marjan.fesb.hr More from this publisher
20.07.2013 Views

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:

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:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!