SQL ORDER BY Keyword
The ORDER BY
clause in SQL is used to sort the result set of a query in either ascending (ASC) or descending (DESC) order.
ORDER BY: Definition and Usage
ORDER BY
is essential for presenting data in a human-readable and organized way. By default, it sorts in ascending order, but you can easily switch to descending order using the DESC
keyword.
Syntax
Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
Example Database
Let's use this sample 'Products' table for the examples:
ProductID | ProductName | SupplierID | CategoryID | Unit | Price |
---|---|---|---|---|---|
1 | Chais | 1 | 1 | 10 boxes x 20 bags | 18 |
2 | Chang | 1 | 1 | 24 - 12 oz bottles | 19 |
3 | Aniseed Syrup | 1 | 2 | 12 - 550 ml bottles | 10 |
4 | Chef Anton's Cajun Seasoning | 2 | 2 | 48 - 6 oz jars | 22 |
5 | Chef Anton's Gumbo Mix | 2 | 2 | 36 boxes | 21.35 |
Examples
Sorting by Price (Ascending)
This query sorts products by price in ascending order (lowest to highest).
Syntax
SELECT * FROM Products
ORDER BY Price;
Output
ProductID | ProductName | SupplierID | CategoryID | Unit | Price
------------------------------------------------------------------------------------------------
3 | Aniseed Syrup | 1 | 2 | 12 - 550 ml bottles | 10
1 | Chais | 1 | 1 | 10 boxes x 20 bags | 18
2 | Chang | 1 | 1 | 24 - 12 oz bottles | 19
5 | Chef Anton's Gumbo Mix | 2 | 2 | 36 boxes | 21.35
4 | Chef Anton's Cajun Seasoning | 2 | 2 | 48 - 6 oz jars | 22
Sorting by Price (Descending)
This query sorts products by price in descending order (highest to lowest).
Syntax
SELECT * FROM Products
ORDER BY Price DESC;
Output
ProductID | ProductName | SupplierID | CategoryID | Unit | Price
------------------------------------------------------------------------------------------------
4 | Chef Anton's Cajun Seasoning | 2 | 2 | 48 - 6 oz jars | 22
5 | Chef Anton's Gumbo Mix | 2 | 2 | 36 boxes | 21.35
2 | Chang | 1 | 1 | 24 - 12 oz bottles | 19
1 | Chais | 1 | 1 | 10 boxes x 20 bags | 18
3 | Aniseed Syrup | 1 | 2 | 12 - 550 ml bottles | 10
Sorting Alphabetically by Product Name
This sorts alphabetically by the 'ProductName' column.
Syntax
SELECT * FROM Products
ORDER BY ProductName;
Output
ProductID | ProductName | SupplierID | CategoryID | Unit | Price
------------------------------------------------------------------------------------------------
3 | Aniseed Syrup | 1 | 2 | 12 - 550 ml bottles | 10
2 | Chang | 1 | 1 | 24 - 12 oz bottles | 19
4 | Chef Anton's Cajun Seasoning | 2 | 2 | 48 - 6 oz jars | 22
5 | Chef Anton's Gumbo Mix | 2 | 2 | 36 boxes | 21.35
1 | Chais | 1 | 1 | 10 boxes x 20 bags | 18
Sorting by Multiple Columns
Sorting can be done by multiple columns; if there are ties in the first column, the second column is used to break the tie.
Syntax
--Example data assumes a 'Customers' table with 'Country' and 'CustomerName' columns.
SELECT * FROM Customers
ORDER BY Country, CustomerName;
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
Output
The first example sorts by country then customer name (ascending). The second example sorts by country (ascending) then customer name (descending).
-- Output will vary depending on your 'Customers' table data