- Filtering with the WHERE Clause
- Sorting with the ORDER BY Clause
- Exam Prep Questions
Sorting with the ORDER BY Clause
So far in this chapter, we have expanded the SELECT statement (including the FROM clause) with the WHERE clause for filtering. Now we will expand the SELECT statement further with the ORDER BY clause. The ORDER BY clause is used to sort rows returned by a query.
CAUTION
The ORDER BY clause is always executed on filtered query results, namely after the WHERE clause. Obviously, if there is no WHERE clause, this does not apply.
ORDER BY Clause Syntax
The syntax for the SELECT statement, including details of the optional ORDER BY clause, is shown in Figure 3.5.
Figure 3.5 ORDER BY clause syntax.
Following is a synopsis of the syntax diagram as shown in Figure 3.5:
The ORDER BY clause is optional and is used for sorting filtered row sets. Thus, a WHERE clause will always appear before an ORDER BY clause.
Elements in the ORDER BY clause can refer to columns, aliased columns, expressions (those expressions can be in the SELECT list or not), or positions of elements in the SELECT list.
Each element in an ORDER BY clause is sorted in ascending order by default. Each element can be forcibly sorted individually in ascending order (ASC) or descending order (DESC).
By default, in ascending order, NULL values will always be sorted last and thus appear last. In descending order NULL values will appear first. Sorted order of NULL values can be overridden using the NULLS FIRST clause and the NULLS LAST clause (the default). NULLS FIRST returns NULL values at the start of a query, and NULLS LAST returns NULL values at the end of a query.
Sorting Using the ORDER BY Clause
Rows in a query are sorted according to the sequence of elements listed in the ORDER BY clause, from left to right. Therefore, the leftmost ORDER BY clause element is the most significant sort parameter, and the rightmost element is the least important sort parameter.
Figure 3.6 shows a query of the ACTOR table sorting by TYPECAST within GENDER.
Figure 3.6 The sequence of ORDER BY clause sorting.
In Figure 3.6 we can see that all females (F) are sorted before all males (M). The TYPECAST column is sorted within GENDER. As a result, Teri Garr as a Comedian appears before George Clooney as an Action Drama actor.
Sorting Methods
The following example sorts on a single column:
SELECT TITLE, RANK, REVIEW_RANK, YEAR FROM MOVIE ORDER BY TITLE;
The next example sorts on multiple columns, not in the order in which the columns are selected:
SELECT TITLE, RANK, REVIEW_RANK, YEAR FROM MOVIE ORDER BY YEAR,TITLE, RANK;
This example sorts columns both in the selected columns list and not in that list but in the accessed MOVIE table:
SELECT TITLE, RANK, REVIEW_RANK, YEAR FROM MOVIE ORDER BY REVIEWS, LIST_PRICE, YEAR, TITLE, RANK;
The next query will produce exactly the same result as the preceding query using positions of select list elements. YEAR is in fourth position, TITLE is in first position, and RANK is in second position:
SELECT TITLE, RANK, REVIEW_RANK, YEAR FROM MOVIE ORDER BY REVIEWS, LIST_PRICE, 4, 1, 2;
If you change the sequence of select list elements, you have to change position numbers as well. This query produces the same rows as before but with the columns in a different order:
SELECT YEAR, TITLE, RANK, REVIEW_RANK FROM MOVIE ORDER BY REVIEWS, LIST_PRICE, 1, 2, 3;
You can sort using aliased columns, even including those not in the select list:
SELECT M.TITLE, M.LIST_PRICE * M.RANK AS PROJSALES FROM MOVIE M ORDER BY M.YEAR, M.TITLE;
And following from the previous query, we can sort using expressions in the select list:
SELECT M.TITLE, M.LIST_PRICE * M.RANK AS PROJSALES FROM MOVIE M ORDER BY PROJSALES;
You can even sort using an expression not in the query select list:
SELECT M.TITLE, M.LIST_PRICE * M.RANK AS PROJSALES FROM MOVIE M ORDER BY M.YEAR / 1000;
NOTE
Sorting using expressions implies that PL/SQL user-defined procedures can be used to sort with as well. PL/SQL is out of the scope of this book.
It follows that we can combine different sorting methods in the same ORDER BY clause. The following example is sorted by a column select list position, a column name in the select list, a column name in the tables but not in the select list, and finally an expression:
SELECT TITLE, RANK, REVIEW_RANK, YEAR, LIST_PRICE * RANK AS PROJSALES FROM MOVIE ORDER BY 4, TITLE, RELEASE, PROJSALES;
Sorting Modifiers
We can modify the way in which each column within an ORDER BY clause is sorted as shown in Figure 3.5, the ORDER BY clause syntax diagram, by using the ASC | DESC and NULLS { FIRST | LAST } optional modifiers.
If you look closely at Figure 3.6 again, you will see that the last two females, Madelaine Kahn and Diane Lane, have no TYPECAST setting. Their TYPECAST column values are NULL because they have not been set to anything, not even a space character. The default setting is NULLS LAST, meaning that NULL values are sorted last, as in Figure 3.6. If we wanted to specify NULLS LAST explicitly, we could use the following query modification:
SELECT GENDER, TYPECAST, NAME FROM ACTOR ORDER BY GENDER, TYPECAST NULLS LAST;
You could return the NULL TYPECAST rows at the beginning of the females by using the following modification:
SELECT GENDER, TYPECAST, NAME FROM ACTOR ORDER BY GENDER, TYPECAST NULLS FIRST;
Now we can reorder the same query. Specify both of the individual columns as being ordered in descending order, placing males before females. Also, specify all TYPECAST values in reverse alphabetical order and NULLS FIRST:
SELECT GENDER, TYPECAST, NAME FROM ACTOR ORDER BY GENDER DESC , TYPECAST DESC NULLS FIRST;
NOTE
A NULL value is logically undefined and thus cannot be sorted, and can be placed only at the start of a sort (NULLS FIRST) or at the end of a sort (NULLS LAST).
This chapter has expanded the SELECT statement with the WHERE clause and the ORDER BY clause to cater for filtering and sorting of returned rows, respectively. The next chapter is the first of two chapters examining some Oracle SQL reference material, essential at this stage, covering operators, conditions, pseudocolumns, and expressions.