WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

SQL ORDER BY Keyword


The ORDER BY keyword is mainly use to sort the result-set.


The SQL ORDER BY Keyword

The ORDER BY keyword is used to sort the result-set by one or more columns.

The ORDER BY keyword mainly to sort result in ascending order by default.

SQL ORDER BY Syntax

SELECT column_name, column_name
FROM table_name
ORDER BY column_name ASC|DESC, column_name ASC|DESC;

Database

In this tutorial we will use the sample database.

Below is a selection from the "Customers" table:

CustomerID FirstName City Country
1

Eva Dallas USA
2 Maria Houston Mexico
3 Rajesh delhi India
4

Lisa TX USA
5 Stephine New york USA

ORDER BY Example

The Define SQL statement selects all customers from the "Customers" table, sorted by the "City" column:

Example

SELECT * FROM Customers
ORDER BY City;

ORDER BY DESC Example

The Define SQL statement selects all customers from the "Customers" table, sorted DESCENDING by the "City" column:

Example

SELECT * FROM Customers
ORDER BY City DESC;

ORDER BY Several Columns Example

The define SQL statement selects all customers from the "Customers" table, sorted by the "Country" and the "FirstName" column:

Example

SELECT * FROM Customers
ORDER BY Country, FirstName;