Database Reference
In-Depth Information
Figure 6.17
ORDER BY sorts columns in ascending order.
By default, ORDER BY sorts in ascending order. You can also specify this by adding the
keyword ASC after the clause. To get the sort to go in descending order we add the keyword
DESC instead, as follows:
SELECT entryid, lastname
FROM visitorbook
ORDER BY lastname DESC
Figure 6.18 shows the results in descending order. You are not limited to just sorting by
one column, and you do not even have to return the column you are sorting from the query.
Here is an example of this which sorts by the lastname but only returns the entryID and
firstname :
SELECT entryid, firstname
FROM visitorbook
ORDER BY lastname, firstname
Compare Figure 6.19 with Figure 6.17. The list is now sorted first on the lastname col-
umn irrespective of whether it is returned or not. After that it is sorted by the firstname
column. This still leaves the three Normans together in the middle of the query, but then
sorts their first names alphabetically as well.
As we have just been discussing the ORDER BY clause, this might seem like a good place
to discuss the GROUP BY clause. However, as much as the two clauses sound similar the
GROUP BY clause has a totally different use and so will be discussed in Chapter 10.
IN
Often in a query you have to search for a specific match, or a match that is less, or more,
than a set value. However, sometimes you may need to search for values that don't easily fit
Search WWH ::




Custom Search