Beginning SQL

Beginning SQL Beginning SQL

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

Chapter 3 SQL Is Set-Based 102 You might remember from your high school math days the concept of the set, which is simply a collection, in no particular order, of items of the same type. SQL is set-based, the sets being sets of records. As with sets in math, there is no particular order to SQL data sets unless you specify the order, using the ORDER BY clause, for example. With the more straightforward queries (like the ones earlier in the chapter), considering the set-based nature of SQL queries isn’t really necessary. However, with trickier queries, especially those involving more than one table, thinking in terms of sets is helpful. Taking the example from the previous section, examine how the first step looks as a set. The first step’s SQL is as follows: SELECT FavCategory.CategoryId, FavCategory.MemberId FROM FavCategory; Represented as a set diagram, the first step looks like Figure 3-2. Figure 3-2 The set simply contains all the records and columns from the FavCategory table. The next step joins the Category and FavCategory tables with this SQL: SELECT Category.Category, FavCategory.MemberId FROM FavCategory INNER JOIN Category ON FavCategory.CategoryId = Category.CategoryId; Figure 3-3 shows a set diagram of the SQL. SELECT FROM FavCategory CategoryId 1 1 1 2 2 3 2 6 1 MemberId 3 5 10 1 3 3 14 14 3

Chapter 3<br />

<strong>SQL</strong> Is Set-Based<br />

102<br />

You might remember from your high school math days the concept of the set, which is simply a collection,<br />

in no particular order, of items of the same type. <strong>SQL</strong> is set-based, the sets being sets of records. As<br />

with sets in math, there is no particular order to <strong>SQL</strong> data sets unless you specify the order, using the<br />

ORDER BY clause, for example. With the more straightforward queries (like the ones earlier in the chapter),<br />

considering the set-based nature of <strong>SQL</strong> queries isn’t really necessary. However, with trickier<br />

queries, especially those involving more than one table, thinking in terms of sets is helpful.<br />

Taking the example from the previous section, examine how the first step looks as a set. The first step’s<br />

<strong>SQL</strong> is as follows:<br />

SELECT FavCategory.CategoryId, FavCategory.MemberId<br />

FROM FavCategory;<br />

Represented as a set diagram, the first step looks like Figure 3-2.<br />

Figure 3-2<br />

The set simply contains all the records and columns from the FavCategory table.<br />

The next step joins the Category and FavCategory tables with this <strong>SQL</strong>:<br />

SELECT Category.Category, FavCategory.MemberId<br />

FROM FavCategory INNER JOIN Category<br />

ON FavCategory.CategoryId = Category.CategoryId;<br />

Figure 3-3 shows a set diagram of the <strong>SQL</strong>.<br />

SELECT FROM FavCategory<br />

CategoryId<br />

1<br />

1<br />

1<br />

2<br />

2<br />

3<br />

2<br />

6<br />

1<br />

MemberId<br />

3<br />

5<br />

10<br />

1<br />

3<br />

3<br />

14<br />

14<br />

3

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

Saved successfully!

Ooh no, something went wrong!