Beginning SQL
Beginning SQL Beginning SQL
Because both conditions (YearReleased and Rating) must be true, an AND statement is required to link them: YearReleased IN (1967, 1977, 1987) AND Rating IN (4,5) Putting the whole statement together gives you the following SQL: SELECT FilmName, YearReleased, Rating FROM Films WHERE YearReleased IN (1967, 1977, 1987) AND Rating IN (4,5); Executing the SQL statement provides the following results: FilmName YearReleased Rating On Golden Puddle 1967 4 Planet of the Japes 1967 5 Soylent Yellow 1967 5 That completes the look at logical operators for this chapter. Chapter 8 discusses logical operators in greater depth. The next section focuses on how to order your query’s results. Ordering Results with ORDER BY So far, query results have come in whatever order the database decides, which is usually based on the order in which the data was entered, unless the database is designed otherwise (as you see in later chapters). However, listing query results in a certain order (a list of names in alphabetical order or a list of years in numerical order) often comes in handy. SQL allows you to specify the order of results with the ORDER BY clause. The ORDER BY clause goes right at the end of the SELECT statement. It allows you to specify the column or columns that determine the order of the results and whether the order is ascending (smallest to largest) or descending (largest to smallest). For example, the following SQL statement displays a list of film years, ordered from earliest to latest: SELECT YearReleased FROM Films ORDER BY YearReleased; Extracting Information By default, ORDER BY sorts into ascending order, which is why the results of the preceding SQL sort from lowest to highest number: 75
- Page 138: Try It Out Using Logical Operators
- Page 142: Exercises 1. Three new members have
- Page 148: Chapter 3 54 MemberId FirstName 1 K
- Page 152: Chapter 3 The results are as follow
- Page 156: Chapter 3 The results to this query
- Page 160: Chapter 3 FavCategory (CategoryId,
- Page 164: Chapter 3 Logical Operators and Ope
- Page 168: Chapter 3 The statement returns the
- Page 172: Chapter 3 NOT Operator Examples thu
- Page 176: Chapter 3 2. Using the following SQ
- Page 180: Chapter 3 When you execute the quer
- Page 184: Chapter 3 Now that you’re acquain
- Page 188: Chapter 3 It’s as simple as that!
- Page 194: YearReleased 1984 1980 1977 1975 19
- Page 198: First, the database system orders t
- Page 202: LastName FirstName DateOfBirth Nigh
- Page 206: The preceding SQL returns the follo
- Page 210: If you put square brackets around i
- Page 214: Finally, if you want to use an alia
- Page 218: Member Name The member is called Ka
- Page 222: FilmName YearReleased Rating Sense
- Page 226: Figure 3-1 The first task when tack
- Page 230: That’s all well and good, but so
- Page 234: Category FirstName LastName War Jen
- Page 238: Extracting Information FirstName La
Because both conditions (YearReleased and Rating) must be true, an AND statement is required to link<br />
them:<br />
YearReleased IN (1967, 1977, 1987)<br />
AND<br />
Rating IN (4,5)<br />
Putting the whole statement together gives you the following <strong>SQL</strong>:<br />
SELECT FilmName, YearReleased, Rating<br />
FROM Films<br />
WHERE<br />
YearReleased IN (1967, 1977, 1987)<br />
AND<br />
Rating IN (4,5);<br />
Executing the <strong>SQL</strong> statement provides the following results:<br />
FilmName YearReleased Rating<br />
On Golden Puddle 1967 4<br />
Planet of the Japes 1967 5<br />
Soylent Yellow 1967 5<br />
That completes the look at logical operators for this chapter. Chapter 8 discusses logical operators in<br />
greater depth. The next section focuses on how to order your query’s results.<br />
Ordering Results with ORDER BY<br />
So far, query results have come in whatever order the database decides, which is usually based on the<br />
order in which the data was entered, unless the database is designed otherwise (as you see in later chapters).<br />
However, listing query results in a certain order (a list of names in alphabetical order or a list of<br />
years in numerical order) often comes in handy. <strong>SQL</strong> allows you to specify the order of results with the<br />
ORDER BY clause.<br />
The ORDER BY clause goes right at the end of the SELECT statement. It allows you to specify the column<br />
or columns that determine the order of the results and whether the order is ascending (smallest to<br />
largest) or descending (largest to smallest). For example, the following <strong>SQL</strong> statement displays a list of<br />
film years, ordered from earliest to latest:<br />
SELECT YearReleased<br />
FROM Films<br />
ORDER BY YearReleased;<br />
Extracting Information<br />
By default, ORDER BY sorts into ascending order, which is why the results of the preceding <strong>SQL</strong> sort<br />
from lowest to highest number:<br />
75