Beginning SQL

Beginning SQL Beginning SQL

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

Chapter 3 92 The preceding SQL produces the same results as before: FilmName YearReleased Rating Sense and Insensitivity 2001 3 15th Late Afternoon 1989 5 Gone with the Window Cleaner 1988 3 The Good, the Bad, and the Facially Challenged 1989 5 The INNER part of INNER JOIN is actually optional in most database systems: INNER JOIN is the default join because it’s the most common. That said, you can write the SQL as follows: SELECT FilmName, YearReleased, Rating FROM Films JOIN Category ON Films.CategoryId = Category.CategoryId WHERE Category.CategoryId = 6; Using INNER JOIN or simply JOIN to create an inner join between tables is not, in fact, the only way to join tables. You may prefer it, though, because INNER JOIN and JOIN make explicit which tables are being joined, and that in turn makes the SQL easier to read and understand. The alternative way of creating an inner join is simply to specify the link in the WHERE clause. Rewriting the preceding SQL by specifying the link looks like this: SELECT FilmName, YearReleased, Rating FROM Films, Category WHERE Films.CategoryId = Category.CategoryId AND Category.CategoryId = 6; The WHERE clause specifies that Films.CategoryId should equal Category.CategoryId, which creates the join. So far, you’ve used the equals operator (=) to join tables, which is termed equijoin. Equijoin is the most common join type, but using any of the other operators is fine. As mentioned earlier, you’re not limited to joining just two tables together in one SELECT statement; within reason, it’s possible to join as many tables as you like. You are sure to encounter a problem that requires joining multiple tables. For example, say you want to produce a list of each film club member’s name and all the films they enjoy based on their favorite film category. In the results, you want to display the members’ first and last names, the name of each film, each film’s year of release, and finally the category in which each film belongs. Now this might seem like a fairly simple problem, but it actually involves the most complex SQL so far in the book. When illustrated step-by-step, however, it’s not so bad at all. Having a diagram of the database tables and their fields often helps when it comes to the more tricky SQL queries. Shown in Figure 3-1 is a diagram of the Film Club database.

Chapter 3<br />

92<br />

The preceding <strong>SQL</strong> produces the same results as before:<br />

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 />

The INNER part of INNER JOIN is actually optional in most database systems: INNER JOIN is the default<br />

join because it’s the most common. That said, you can write the <strong>SQL</strong> as follows:<br />

SELECT FilmName, YearReleased, Rating<br />

FROM Films JOIN Category<br />

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

WHERE Category.CategoryId = 6;<br />

Using INNER JOIN or simply JOIN to create an inner join between tables is not, in fact, the only way to<br />

join tables. You may prefer it, though, because INNER JOIN and JOIN make explicit which tables are<br />

being joined, and that in turn makes the <strong>SQL</strong> easier to read and understand. The alternative way of creating<br />

an inner join is simply to specify the link in the WHERE clause. Rewriting the preceding <strong>SQL</strong> by<br />

specifying the link looks like this:<br />

SELECT FilmName, YearReleased, Rating<br />

FROM Films, Category<br />

WHERE Films.CategoryId = Category.CategoryId AND<br />

Category.CategoryId = 6;<br />

The WHERE clause specifies that Films.CategoryId should equal Category.CategoryId, which creates the<br />

join.<br />

So far, you’ve used the equals operator (=) to join tables, which is termed equijoin. Equijoin is the most<br />

common join type, but using any of the other operators is fine.<br />

As mentioned earlier, you’re not limited to joining just two tables together in one SELECT statement;<br />

within reason, it’s possible to join as many tables as you like. You are sure to encounter a problem that<br />

requires joining multiple tables. For example, say you want to produce a list of each film club member’s<br />

name and all the films they enjoy based on their favorite film category. In the results, you want to display<br />

the members’ first and last names, the name of each film, each film’s year of release, and finally the<br />

category in which each film belongs.<br />

Now this might seem like a fairly simple problem, but it actually involves the most complex <strong>SQL</strong> so far<br />

in the book. When illustrated step-by-step, however, it’s not so bad at all.<br />

Having a diagram of the database tables and their fields often helps when it comes to the more tricky<br />

<strong>SQL</strong> queries. Shown in Figure 3-1 is a diagram of the Film Club database.

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

Saved successfully!

Ooh no, something went wrong!