SQL

Sorting Data with ORDER BY

Learn how to sort query results in ascending or descending order.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Learn how to sort query results in ascending or descending order. This hands-on tutorial focuses on practical implementation of sorting data with order by concepts.

Sorting Results

By default, SQL does not guarantee the order of returned rows. To sort the result set, use the ORDER BY clause.

Syntax

SELECT column1, column2
FROM table_name
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC];
  • ASC: Ascending order (default). A-Z, 0-9.
  • DESC: Descending order. Z-A, 9-0.

Example: Sort by Name

Sort customers alphabetically by their name.

SQL PLAYGROUND
⏳ Loading editor…

Example: Sort Descending

Sort products by price from highest to lowest.

SQL PLAYGROUND
⏳ Loading editor…

Sorting by Multiple Columns

You can sort by more than one column. This is useful when the first column has duplicate values.

SQL PLAYGROUND
⏳ Loading editor…

In this example, customers are sorted by Country first. If two customers are from the same country, they are then sorted by their Name.