SELECT statement retrieves rows from the database and has the most complex structure among other SQL statements. Almost any database user is capable of writing a simplest SELECT statement such as
The vertical projection of the �C table is obtained by listing the necessary fields only. For example, to get information about the processor speed and the amount of RAM in the computer run the following query:
It should be noted that a vertical sample may include duplicate rows in case where the sample does not include any potential key with the values uniquely identify each row in the table. In the PC table, the code field is a potential key, which is specified in addition as primary key. Since this field is not included in the query, there are listed some duplicate rows in the above result set (for example, rows 1 and 3). If unique rows are needed (say, we only need different combinations of processor speed and RAM amount, not specifications of all available PCs), use the DISTINCT keyword:
Apart from DISTINCT, the ALL keyword, which explicitly ask for all rows, may also be applicable. However, ALL keyword is accepted by default.
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 nonnegative
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 result set can be sorted in ascending order (ASC is assumed by default) or in descending order (DESC). 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: