5.PracticalSQL2E_SampleCh3 - Copy
5.PracticalSQL2E_SampleCh3 - Copy
the SELECT clause. Thus, you could rewrite Listing 3-3 this way, using 3 to
refer to the third column in the SELECT clause, salary:
The ability to sort in our queries gives us great flexibility in how we view
and present data. For example, we’re not limited to sorting on just one col-
umn. Enter the statement in Listing 3-4:
In this case, we’re retrieving the last names of teachers, their school,
and the date they were hired. By sorting the school column in ascending
order and hire_date in descending order 1, we create a listing of teachers
grouped by school with the most recently hired teachers listed first. This
shows us who the newest teachers are at each school. The result set should
look like this:
You can use ORDER BY on more than two columns, but you’ll soon reach
a point of diminishing returns where the effect will be hardly noticeable.
Imagine if you added columns about teachers’ highest college degree
attained, the grade level taught, and birthdate to the ORDER BY clause. It
would be difficult to understand the various sort directions in the output
all at once, much less communicate that to others. Digesting data happens
most easily when the result focuses on answering a specific question; there-
fore, a better strategy is to limit the number of columns in your query to
only the most important and then run several queries to answer each ques-
tion you have.