WiseOwl Training - Established 1992 Wise Owl Training

Established May 1992
30 years in business
Wise Owl Training
30 years in business
See 479 reviews for our classroom and online training
Using ORDER BY to Sort in SQL Server Queries
Part one of a two-part series of blogs

If your SQL queries return lots of records you'll probably want to sort the results in some kind of order. This blog teaches you how to use the ORDER BY clause to do exactly that!

  1. Sorting in SQL Server Queries (this blog)
  2. Finding the Top Records in a Query

This article is part of our complete SQL tutorial.  You might also be interested in our introduction to SQL or advanced SQL training courses.

Posted by Andrew Gould on 23 July 2012

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Sorting in SQL Server Queries

This blog teaches you how to sort the results of basic queries in Microsoft SQL Server.

The ORDER BY Clause

You can sort the results of a query by adding the ORDER BY clause to a SELECT statement.  If you've read our blog on basic SQL queries you'll remember that ORDER BY is the last of the six main keywords in a SQL query.  If you haven't read the previous blog, here's a quick reminder of the order of keywords:







Sorting Queries Using Column Names

To sort the results of a query you can simply add a column name to the ORDER BY clause:

Sorting by one column name

Adding the FilmName field to the ORDER BY clause sorts the query results in ascending order of film title.

By default, a column will be sorted in ascending order.  You can add the ASC keyword to make this more obvious:

Using ASC

The ASC keyword is optional but makes the query more readable.


You can sort a column in descending order by adding the DESC keyword after the column name:

Sorting in descending order

Adding the DESC keyword sorts a column in descending order.

Sorting on Multiple Columns

You can sort your query on multiple columns by listing extra column names separated with commas:

Sorting by multiple columns

Here the films are sorted in descending order of release date. Where multiple films share a release date they are sorted in ascending order of name.


Sorting Using Non-Selected Columns

You don't have to include a column in the SELECT list in order to use it in the ORDER BY clause:

Sorting with nonselected column

The results of this query are sorted so that the most successful Oscar winners appear at the top, even though that field isn't shown in the query.


Sorting Queries Using Aliases

If you've used aliases to replace long column names in the SELECT list you can also use them in the ORDER BY clause:

Sorting using alias

Here we've used the alias to sort the films in descending order of duration.

Using an alias is also a useful way to sort on a calculated column.

What's Next?

Now that you've seen the basics of changing the order of query results it's time to see how to return a given number of items from a table using the TOP keyword.  Read the next part of this series to find out how.

  1. Sorting in SQL Server Queries (this blog)
  2. Finding the Top Records in a Query
This blog has 0 threads Add post