Beginning SQL

Beginning SQL Beginning SQL

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

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

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

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

Saved successfully!

Ooh no, something went wrong!