Order columns by column number

To order by a column defined by an expression, use its column number, as in this example:

SELECT ID, NAME, SALARY+COMM
FROM Q.STAFF
WHERE COMM IS NOT NULL
ORDER BY 3

You cannot use an expression like SALARY+COMM after ORDER BY.

You can use more than one column number in a list after ORDER BY, and you can use column names and column numbers in the same list. For example, in the query above, SALARY+COMM is column 3 and NAME is column 2. The last line of the query can be written:

ORDER BY 3 DESC, NAME

To list employees in descending order by salary within a department:

SELECT DEPT, ID, NAME, SALARY
FROM Q.STAFF
ORDER BY 1, 4 DESC
[ Previous Page | Next Page | Contents | Index ]