20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

FilmName YearReleased Rating<br />

Sense and Insensitivity 2001 3<br />

15th Late Afternoon 1989 5<br />

Gone with the Window Cleaner 1988 3<br />

The Good, the Bad, and the Facially Challenged 1989 5<br />

You might argue that if your database has only six categories, looking up each category is not that hard or<br />

time-consuming. It’s a different story altogether, though, if your database contains 50 or 100 categories.<br />

Also, while computers might be more at home with numbers, most humans prefer names. For example,<br />

imagine that you create a film club Web site that contains a page allowing users to choose a category<br />

and then display all the films for that category. It’s unlikely that the Web site user would want to choose<br />

categories based on category IDs. What’s more likely is that users would choose by name and allow the<br />

database to work out the ID and display the results.<br />

That said, how can you use a join to obtain a list of films in the Historical category?<br />

First, you need to determine which table contains category names and allows you to look up the<br />

CategoryId. From the previous example, it’s clearly the Category table that provides this information.<br />

Second, you need to determine which table or tables provide the results you want. Again, based on the<br />

preceding example, you know that the Films table contains the data needed. The task now is to join<br />

the two tables together. To join the tables, you need to find a link between the two tables. No prizes for<br />

guessing that the CategoryId field, which is present in both tables, links the two!<br />

The type of join to use in this case is an inner join. An inner join combines two tables and links, or joins,<br />

them based on columns within the tables. The inner join allows you to specify which columns form the<br />

join and under what conditions. For example, you could specify a condition that says the MemberId column<br />

in the MemberDetails table matches a value from the MemberId column in the FavCategory table.<br />

Then only records where there is a matching MemberId in both tables are included in the results. To create<br />

an inner join, you must specify the two tables to be joined and the column or columns on which the join is<br />

based. The syntax looks like this:<br />

table1 INNER JOIN table2 ON column_from_table1 = column_from_table2<br />

Applying the syntax to the problem at hand yields the following code:<br />

SELECT FilmName, YearReleased, Rating<br />

FROM Films INNER JOIN Category<br />

ON Films.CategoryId = Category.CategoryId<br />

WHERE Category.CategoryId = 6;<br />

Extracting Information<br />

At the top is the SELECT statement’s list of the columns required to form the results. On the following<br />

line are the tables from which the results are drawn. Specify them as normal, except this time the INNER<br />

JOIN keyword specifies that the two tables should be joined. The ON keyword that follows specifies what<br />

joins the tables; in this case, the CategoryId field joins them. Note that you must specify the table names<br />

in front of the CategoryId, otherwise the database doesn’t know if you mean the CategoryId in the Films<br />

table or the CategoryId in the Category table. In fact, the two columns don’t actually need to have the<br />

same name, depending on the database’s design. Designing a database that way doesn’t make sense,<br />

though; when someone looks at the database design, they should be able to see that CategoryId in each<br />

table relates to the same set of values.<br />

91

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

Saved successfully!

Ooh no, something went wrong!