Back to SQL Notes
Topic #7

SQL Order By

Sorted output is what makes reports readable, and ORDER BY gives you full control over it. Learn to arrange rows by one or more columns in ascending or descending order.


The SQL ORDER BY

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

The ORDER BY keyword sorts the result-set in ascending order (ASC) by default.

Example

SELECT * FROM Products

ORDER BY Price;

ORDER BY Syntax

<p><code class="sqlHigh">
SELECT <em>column1</em>,<em> column2, ...</em><br/>
FROM <em>table_name</em><br/>
ORDER BY <em>column1, column2, ... </em>ASC|DESC;
</code></p>

Demo Database

Below is a selection from the Products table used in 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

ORDER BY DESC

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

Example

SELECT * FROM Products

ORDER BY Price DESC;

Order Alphabetically

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

Example

SELECT * FROM Products

ORDER BY ProductName;

Alphabetically DESC

To sort the text values in a column in a descending order, use the DESC keyword:

Example

SELECT * FROM Products

ORDER BY ProductName DESC;

ORDER BY Several Columns

The following SQL statement selects all customers from the "Customers" table - and sorts it by the "Country" and the "CustomerName" column.

This means that it sorts it first by Country, and if some records have the same Country, it sorts them by CustomerName:

Example

SELECT * FROM Customers

ORDER BY Country, CustomerName;

Combine ASC and DESC

The following SQL statement selects all customers from the "Customers" table, and sorts it ASCENDING by the "Country" and DESCENDING by the "CustomerName" column:

Example

SELECT * FROM Customers

ORDER BY Country ASC, CustomerName DESC;

Want to go beyond the notes?

Join Coding Now Tech Institute's SQL course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

SQL Order By – FAQs

Quick answers about learning SQL Order By in SQL.

This free note from Coding Now Tech Institute explains SQL Order By in SQL — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every SQL topic on Coding Now Tech Institute, including SQL Order By, is 100% free with no signup required.
With focused practice, most students grasp SQL Order By in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now