bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/SQL/SQL Tutorial
SQL•SQL Tutorial

SQL ORDER BY Keyword

The SQL ORDER BY

The ORDER BY

Formula

keyword is used to sort the result - set in ascending or descending order.

The ORDER BY

Formula

keyword sorts the result - set in ascending order

(ASC) by default.

Example

Sort the products from lowest to highest price:

SELECT * FROM Products
ORDER BY Price;

ORDER BY Syntax

Select

column1, column2, ...

From

table_name

Order By

column1, column2, ...

ASC|DESC;

Demo Database

Below is a selection from the

Products table used in the examples:

ProductID

ProductName

SupplierID

CategoryID

Unit

Price

Chais

10 boxes x 20 bags 18

Chang

Formula

24 - 12 oz bottles

19

Aniseed Syrup

Formula

12 - 550 ml bottles

10

Chef Anton's Cajun Seasoning

Formula

48 - 6 oz jars

22

Chef Anton's Gumbo Mix

36 boxes 21.35

Order By Desc

To sort the records in descending order, use the DESC keyword.

Example

Sort the products from highest to lowest price:

SELECT * FROM Products
ORDER BY Price DESC;

Order Alphabetically

For string values, the ORDER BY keyword will sort the values in the column alphabetically:

Previous

SQL WHERE Clause

Next

SQL AND Operator