Simple SELECT statement

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

SELECT * FROM PC;
mssql
🚫
[[ error ]]
[[ column ]]
[[ value ]]
which retrieves all records from the table-type object PC; in so doing rows and columns of the result set have no order. To order columns of the result set they should be listed and separated by commas in the required order after the SELECT keyword:

SELECT price, speed, hd, ram, cd, model, code
FROM PC;
mssql
🚫
[[ error ]]
[[ column ]]
[[ value ]]

Here is the result set of this query.

pricespeedhdramCdmodelcode
60050056412x12321
8507501412840x11212
60050056412x12333
8506001412840x11214
850600812840x11215
9507502012850x12336
400500103212x12327
35045086424x12328
350450103224x12329
350500103212x126010
9809004012840x123311

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:

SELECT speed, ram
FROM PC;
mssql
🚫
[[ error ]]
[[ column ]]
[[ value ]]
which returns the data:

speedram
50064
750128
50064
600128
600128
750128
50032
45064
45032
50032
900128

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:

SELECT DISTINCT speed, ram
FROM PC;
mssql
🚫
[[ error ]]
[[ column ]]
[[ value ]]

Here’s the result set:

speedram
45032
45064
50032
50064
600128
750128
900128

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 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

SELECT DISTINCT speed, ram
FROM PC
ORDER BY ram DESC;
mssql
🚫
[[ error ]]
[[ column ]]
[[ value ]]
or

SELECT DISTINCT speed, ram
FROM PC
ORDER BY 2 DESC;
mssql
🚫
[[ error ]]
[[ column ]]
[[ value ]]

The following result is the same for both above queries.

speedram
600128
750128
800128
900128
45064
50064
45032
50032

The result set can be sorted in ascending order (ASC is assumed by default) or in descending order (DESC keyword).

Note

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

SELECT *
FROM PC
ORDER BY 3;
mssql
🚫
[[ error ]]
[[ column ]]
[[ value ]]
can give absolutely another sequence or generally cause an error, referring to an absent column.

Sorting by two columns

SELECT DISTINCT speed, ram
FROM PC
ORDER BY ram DESC, speed DESC;
mssql
🚫
[[ error ]]
[[ column ]]
[[ value ]]
gives the following result:

speedram
900128
800128
750128
600128
50064
45064
50032
45032

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:

SELECT DISTINCT speed, ram
FROM PC
WHERE price < 500
ORDER BY 2 DESC;
mssql
🚫
[[ error ]]
[[ column ]]
[[ value ]]
speedRam
45064
45032
50032

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:

PredicateDescription
price < 1000Price is less than 1000
type = ‘laptop’Product type is Laptop
cd = ‘24x’24-speed CD-ROM
color <> ’y’Not-color printer
ram – 128 > 0RAM amount is over 128 Mb
Price <= speed*2Price does not exceed twice processor’s speed

Suggested exercises: 1, 2, 3, 4, 5, 6, 9, 14, 31, 33, 42.

Sorting can be accomplished by the columns absent from SELECT column-list. Naturally, these columns should be presented in the output of FROM clause. For example, to deduce the model list of PCs in the order from greatest price to lowest one, you can write

select model from PC
order by price DESC;
mssql
🚫
[[ error ]]
[[ column ]]
[[ value ]]

Notice that the price itself does not be returned by the query. Elimination of duplicates produces ambiguous situation that prevents the behaviour. Thus, the query

select DISTINCT model from PC
order by price DESC;
mssql
🚫
[[ error ]]
[[ column ]]
[[ value ]]
gives us the error yet:

ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

The same reason prevents from unerror working of the following query that uses grouping

select model from PC
group by model
order by price DESC;
mssql
🚫
[[ error ]]
[[ column ]]
[[ value ]]
Column "PC.price" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.

But if you eject ambiguity (i.e. to do sorting by an aggregate-function value for a group), the query will work:

select model from PC
group by model
order by MAX(price) DESC;
mssql
🚫
[[ error ]]
[[ column ]]
[[ value ]]

Note

All the query examples (including erroneous ones) will work in MySQL, which eliminates ambiguity by itself. Do you want to know how? Look in MySQL documentation. :-)