Microsoft Docs

Documentation for SQL Server

SQL ORDER BY Clause

The ORDER BY clause in SQL is used to sort the result set of a query in ascending or descending order. This clause is optional and is typically used at the end of a SELECT statement.

Syntax

The basic syntax for the ORDER BY clause is as follows:


SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;
            

How it Works

When you use ORDER BY, the database engine processes the query and then arranges the returned rows according to the specified column(s) and sort order. You can sort by one or more columns. If you specify multiple columns, the sorting will be applied sequentially. The rows will be sorted by the first column, and then for any rows with identical values in the first column, they will be sorted by the second column, and so on.

Example 1: Sorting by a Single Column (Ascending)

Let's say you have a table named Customers with columns CustomerID, FirstName, and City. To retrieve all customers and sort them by their first name in ascending order:


SELECT CustomerID, FirstName, City
FROM Customers
ORDER BY FirstName ASC;
                

If ASC is omitted, the result is the same as ORDER BY FirstName.

Example 2: Sorting by a Single Column (Descending)

To sort the same Customers table by City in descending order:


SELECT CustomerID, FirstName, City
FROM Customers
ORDER BY City DESC;
                

Example 3: Sorting by Multiple Columns

To sort customers first by City (ascending) and then by FirstName (ascending) for customers in the same city:


SELECT CustomerID, FirstName, City
FROM Customers
ORDER BY City ASC, FirstName ASC;
                

This will list all customers from 'Berlin', sorted alphabetically by their first name, then all customers from 'London', sorted alphabetically by their first name, and so on.

Sorting by Column Position

You can also sort by the ordinal position of the column in the SELECT list. This is less readable and generally not recommended for maintainability, but it is supported.

Example 4: Sorting by Column Position

Sorting by the second column (FirstName) in ascending order:


SELECT CustomerID, FirstName, City
FROM Customers
ORDER BY 2 ASC;
                

Sorting Based on Expressions

The ORDER BY clause can also sort data based on an expression.

Example 5: Sorting by an Expression

Concatenate FirstName and LastName and sort by the combined name:


SELECT CustomerID, FirstName, LastName
FROM Customers
ORDER BY FirstName + ' ' + LastName ASC;
                

NULL Values

NULL values are typically treated as the lowest values when sorting in ascending order and the highest values when sorting in descending order, depending on the specific SQL database system. However, some systems provide explicit ways to control the handling of NULL values.

Tip: Handling NULLs

In SQL Server, you can use NULLS FIRST or NULLS LAST (though these are not standard SQL and may not be supported by all RDBMS). For standard SQL, you might use a CASE statement or rely on the default behavior.


-- Example for SQL Server (non-standard syntax)
SELECT column1
FROM table_name
ORDER BY column1 DESC NULLS FIRST;
                

Performance Considerations

Understanding and effectively using the ORDER BY clause is fundamental for presenting data in a structured and meaningful way in your SQL queries.