LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 1815 min read

ORDER BY

Learn how to sort query results in SQL Server using ORDER BY, including ascending and descending order and sorting by multiple columns.

Introduction

By default, SQL Server does not guarantee any particular order for the rows returned by a query — the order depends on internal storage and execution details that can change. If you need a predictable, meaningful order, such as newest orders first or employees sorted alphabetically, you must use the ORDER BY clause. This lesson covers how to sort results ascending, descending, and across multiple columns.

What You Will Learn
  • Why ORDER BY is required for predictable row order.
  • How to sort ascending and descending.
  • How to sort by multiple columns.
  • How to reference columns by position in ORDER BY.

Basic ORDER BY

ORDER BY comes after the WHERE clause (if present) and specifies which column, or columns, to sort the result set by. By default, sorting is ascending.

Basic ORDER BY
SELECT FirstName, Department, Salary
FROM Employees
ORDER BY Salary;
Result

Click Run to see what this code prints.

Ascending vs Descending

Use ASC for ascending order (the default, so it is often omitted) and DESC for descending order. DESC is especially common when you want the newest, highest, or largest values first.

ASC and DESC
SELECT FirstName, Salary
FROM Employees
ORDER BY Salary DESC;
Result

Click Run to see what this code prints.

Ordering by Multiple Columns

You can sort by more than one column by separating them with commas. SQL Server sorts by the first column, and for any rows that tie on that column, it uses the next column as a tiebreaker, and so on. Each column can have its own ASC or DESC direction.

Multi-Column ORDER BY
SELECT FirstName, Department, Salary
FROM Employees
ORDER BY Department ASC, Salary DESC;
Result

Click Run to see what this code prints.

NULLs Sort First

In SQL Server, NULL values are treated as the lowest possible value and appear first in an ascending sort (and last in a descending sort).

Ordering by Column Position

T-SQL also allows you to reference columns in ORDER BY by their ordinal position in the SELECT list, rather than by name. This is convenient for quick queries but is discouraged in production code because it silently breaks if the column list changes.

Ordering by Position
SELECT FirstName, Department, Salary
FROM Employees
ORDER BY 3 DESC; -- sorts by Salary, the 3rd column in the SELECT list
Result

Click Run to see what this code prints.

Common Mistakes

Avoid These Mistakes
  • Assuming rows come back in a consistent order without ORDER BY — SQL Server makes no such guarantee.
  • Forgetting that DESC applies only to the column immediately before it, not the whole list.
  • Using ordinal position ordering in production queries, which breaks silently when the SELECT list changes.
  • Placing ORDER BY before WHERE or GROUP BY, which is invalid — ORDER BY must be the last clause in the query.

Best Practices

  • Always use ORDER BY explicitly whenever row order matters to your application.
  • Reference columns by name rather than ordinal position for clarity and maintainability.
  • Specify ASC or DESC explicitly per column when sorting by multiple columns, even though ASC is the default.
  • Combine ORDER BY with TOP to efficiently retrieve "top N" style results.

Frequently Asked Questions

Sorting has a cost, especially on large result sets without a supporting index. SQL Server may use an existing index to avoid an explicit sort when possible.

Yes, in most cases you can sort by any column from the underlying tables, even if it is not part of the SELECT output — unless the query uses DISTINCT or GROUP BY, which restrict this.

NULLs are treated as the lowest value, so with DESC they appear last, and with ASC they appear first.

Key Takeaways

  • Without ORDER BY, row order is not guaranteed.
  • ASC sorts ascending (default); DESC sorts descending.
  • Multiple columns can be used, with later columns breaking ties.
  • NULLs sort as the lowest value.
  • ORDER BY must be the last clause in a query.

Summary

ORDER BY gives you full control over how your query results are sorted, whether by one column or several. Next, you will combine sorting with SQL Server's TOP clause to retrieve only a limited number of rows.

Next Lesson →

TOP Clause