Simple SELECT statement page 2
It is possible to sort out the result set by a number of columns pointed out in the SELECT statement. For this purpose, the clause ORDER BY <list of fields> is used which is always the latest clause in the SELECT statement. In so doing, the sort column in list of fields may be specified as a name or a non negative integer representing the position of the name in SELECT list. For example, to sort the result set by RAM in descending order we can write
The following result is the same for both above queries.
The result set can be sorted in ascending order (ASC is assumed by default) or in descending order (DESC keyword).
It is not recommended to use in applications the queries with sorting by numbers of columns. This is connected with the fact that the structure of a table can change over time, for example, as a result of addition/removal of columns. As consequence, the following query
Sorting by two columns
Horizontal restriction is realized by the clause WHERE <predicate> after the FROM clause. Now the result set will only include the rows from the record source for each of those the predicate returns TRUE. In other words, the predicate for each row is checked . For example, the query "get information about processor's speed and RAM amount for computers priced below $500" can be written as follows:
The latter query uses a comparison predicate with operator "<" (less than). Beside this operator, the following operators may be used: "=" (equal), ">" (greater than), ">="(greater or equal), "<=" (less or equal) and "<>" (not equal). Expressions in comparison predicates may include any columns from the tables listed in the FROM clause. Character strings and date/time constants are enclosed in single quotation marks.
Here are some examples of simple comparison predicates: