ORDER BY (sorting the Results), Part 2 of 2
The sort sequence for numeric data, dates, and times is easily obvious.
When you look at the sort sequence for alphanumeric data with special characters, the order is sometimes not so obvious. See Collating Sequence.
If the ORDER BY columns do not identify each row uniquely, rows with duplicate values in the ORDER BY clause will be listed in arbitrary order.
You can use expressions in place of the ORDER BY columns.Nulls complicate the situation.
Even though nulls mean that there is no value, nulls are considered equal when sorted.
The SQL standard leaves it up to the RDBMS system to decide if nulls are greater or less than non-null values.
SQL allows you to use a relative column number in place of the column name in the ORDER BY clause.
This relative column number points to the column name in the SELECT clause.
This practice is a bad idea because the order of the SELECTed columns could be changed, and this would mean that the relative column numbers now point to different column names and is confusing.
You can reference column alias names in the ORDER BY clause. See: AS (Column Aliases).
You can intermingle, column names, alias column names, expressions, and relative column numbers.
> Access, MySQL, and SQL Server treat nulls as the lowest possible value.
> DB2, Oracle, and PostgreSQL treat nulls as the highest value.
> Access won’t let you sort expressions.
> Oracle won’t let you sort BLOB, CLOB, or NCLOB.
> SQL Server won’t let you sort IMAGE, TEXT, or NTEXT.