Order By SQL | SQL Order By Clause of The SQL Statement With Example
Summary: In this article, you will learn ORDER BY SQL and how to use the Order By Clause of The SQL Statement With Example to sort the result and set a query by one or more columns.
The ORDER BY SQL Keyword
- The ORDER BY SQL keyword is used for the purpose of sorting the result-set in ascending or descending order to one or more columns.
- The ORDER BY SQL keyword generally sorts the records in ascending order by default. When you want to sort the records in descending order, then you need to use the DESC keyword and the keyword ASC to sort in ascending order.
ORDER BY Syntax
FROM table_name
ORDER BY column1, column2, … ASC|DESC;
DESC: The keyword DESC will be used to sort the records in descending order
Demo Database for ORDER BY SQL
| ClientsID | ClientsName | ClientsAge | Address | City | PostalCode | Country |
|---|---|---|---|---|---|---|
| 11 | Aaron Brayden | 31 | Berguvsvägen | Lulea | 345212 | Sweden |
| 12 | Coinneach Dev | 38 | Mataderos 2312 | México D.F. | 5023 | Mexico |
| 13 | Devin Diego | 40 | 140 Hanover Sq. | London | 18909 | UK |
| 14 | Ethan Driss | 43 | Avda. de la Constitución 231 | México D.F. | 5021 | Mexico |
| 15 | Alfreds Futterkiste | 35 | Obere Str. 57 | Berlin | 12209 | Germany |
1. ORDER BY Example
The given below following SQL statement selects all Clients from the “Clients” table, sorted by the “City” column:
|
1 2 |
SELECT * FROM Clients ORDER BY City; |

2. SQL ORDER BY Ascending Sort according to one column:
To sort the records in ascending order in SQL, we can use the keywords ASC.
|
1 |
SELECT * FROM Clients ORDER BY ClientsName ASC |

3. SQL ORDER BY Descending Sort according to one column:
To sort the records in descending order in SQL, we can use the keywords DESC.
|
1 |
SELECT * FROM Clients ORDER BY ClientsName DESC |

4. Sort according to multiple columns:
Here, we will use the ORDER BY to sort in ascending or descending order then you can use the keywords ASC or DESC respectively. To sort records according to multiple columns, separate the names of columns by the (,) operator.
|
1 2 |
SELECT * FROM Clients ORDER BY ClientsName ASC, City DESC; |

5. ORDER BY Several Columns Example
The following SQL statement selects all clients from the “Clients” table, sorted by the “Country” and the “ClientsName” column. It implies that it orders by Country, but if some rows have the same Country, it orders them by ClientsName:
|
1 2 |
SELECT * FROM Clients ORDER BY Country, ClientsName; |

There are Six Operations are used to Order: SELECT, FROM, WHERE, GROUP BY, HAVING, and ORDER BY
6. Sorting Results by the relative position
To sort by relative position in the result set then you can also use the ORDER BY clause. whereas the first field in the result set is 1, & the second field is 2, and so on.
In this example, we have a table called clients with the following data:
|
1 2 3 4 |
SELECT CliensID, ClientsName FROM Clients WHERE ClientsName < > 'Dev' ORDER BY 1 DESC; |

Conclusion:
In this article, you have learned how to sorted the records in ascending or descending order with the help of the ORDER BY clause and with the different types of examples. I hope you will enjoy it!