Specifying the ORDER BY Using Column Aliases
Alternatively, you can use a column alias to specify the columns used in the ORDER BY
clause. This is the preferred method for sorting by an expression
because it makes the code easier to read. This example sorts in
descending order, rather than the default ascending order:
USE AdventureWorks
SELECT LastName + ‘, ‘ + FirstName as FullName
FROM Person.Person
ORDER BY FullName DESC;
Result:
FullName
-------------
Zwilling, Michael
Zwilling, Michael
Zukowski, Jake
Zugelder, Judy
Zubaty, Patricia
Zubaty, Carla
Zimprich, Karin
. . .
An alias is allowed in the ORDER BY clause but not the WHERE clause because the WHERE clause is logically executed prior to processing columns and expressions. The ORDER BY clause follows the assembling of the columns and aliases, so it can use column aliases.
Using the Column Ordinal Position
You can use the ordinal number of the column (column position number) to indicate the ORDER BY columns, but don't do this. If the select columns are changed or their order changes, the sort order also changes.
One case for which it's not necessarily a horrid
practice to use the ordinal number to specify the sort is for complex
union queries.
The following query demonstrates sorting by ordinal position:
USE AdventureWorks
SELECT LastName + ‘, ‘ + FirstName as FullName
FROM Person.Person
ORDER BY 1;
Result:
FullName
----------------------
Abbas, Syed
Abel, Catherine
Abercrombie, Kim
. . .
ORDER BY and Collation
SQL Server's collation order is vital
to sorting data. In addition to determining the alphabet, the collation
order also determines whether accents, case, and other alphabet
properties are considered in the sort order. For example, if the
collation is case-sensitive, then the uppercase letters are sorted
before the lowercase letters. The following function reports the
installed collation options and the current collation server property:
SELECT * FROM fn_helpcollations();
Result:
name description
--------------------- -------------------------
Albanian_BIN Albanian, binary sort
Albanian_CI_AI Albanian, case-insensitive,
accent-insensitive,
kanatype-insensitive, width-insensitive
Albanian_CI_AI_WS Albanian, case-insensitive,
accent-insensitive,
kanatype-insensitive, width-sensitive
. . .
SQL_Latin1_General_CP1_CI_AI
Latin1-General,case-insensitive,
accent-insensitive,
kanatype-insensitive, width-insensitive
for Unicode Data, SQL Server Sort Order
54 on Code Page 1252 for non-Unicode
Data
. . .
The following query reports the current server collation:
SELECT SERVERPROPERTY('Collation') AS ServerCollation;
Result:
ServerCollation
------------------------
SQL_Latin1_General_CP1_CI_AS
Although the server collation setting is
determined during setup, you can set the collation property for a
database or a column using the COLLATE keyword. The following code changes the AdventureWorks database collation so that it becomes case-sensitive:
CREATE DATABASE CollateChange
GO
ALTER DATABASE CollateChange
COLLATE SQL_Latin1_General_CP1_CS_AS;
GO
SELECT DATABASEPROPERTYEX('CollateChange','Collation')
AS DatabaseCollation;
Result:
DatabaseCollation
----------------------------------
SQL_Latin1_General_CP1_CS_AS
Not only can SQL Server set the collation at the
server, database, and column levels, collation can even be set at the
individual query level. The following query sorts according to the
Danish collation, without regard to case or accents:
Use AdventureWorks
SELECT *
FROM Production.Product
ORDER BY Name
COLLATE Danish_Norwegian_CI_AI;
Not all queries need to be sorted, but for those that do, the ORDER BY clause combined with the many possible collations yields tremendous flexibility in sorting the result set.
Terminating the Statement
ANSI SQL uses a semicolon to terminate
a statement. Although it's been there as an option for several
versions, code with semicolons was unheard of in the SQL Server
community until recently. SQL Server 2005 began requiring it for some
commands. Therefore, following are the rules about semicolons.
When semicolons are required:
- At the end of the statement preceding a common table expression (CTE)
- At the end of a MERGE statement
When not to use a semicolon:
- Between the END TRY and BEGIN CATCH.
- Between the IF condition and the BEGIN.
- Don't mix GO and the semicolon on the same line.