Beginning SQL
Beginning SQL Beginning SQL
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
- 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 220: Chapter 3 Member Name The member is
- Page 224: Chapter 3 92 The preceding SQL prod
- Page 228: Chapter 3 94 ❑ Category ❑ FavCa
- Page 232: Chapter 3 96 You’re one step furt
- Page 236: Chapter 3 98 FirstName LastName Cat
- Page 240: Chapter 3 This time, you achieve mo
- 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
- Page 270: FirstName LastName DateOfBirth Jenn
- Page 274: 4 Advanced Database Design This cha
- Page 278: Date of Date Meeting Did Member Nam
- Page 282: Now the tables are in second normal
- Page 286: After changing the database so that
- Page 290: IBM’s DB2 doesn’t allow you to
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