codingstreets
Search
Close this search box.

Get Started: SQL ORDER BY

In This Article You Will Know About SQL ORDER BY. 

SQL ORDER BY – Before moving ahead, let’s know a bit about SQL Operators.

Table of Contents

SQL ORDER BY

The ORDER BY keyword is used in SQL to arrange the records ascending or descending order.

By default, the records are sorted in ascending order; use the ‘DESC’ keyword to sort in descending order.

ORDER BY Syntax

SELECT column1, column2, …
FROM table_name
ORDER BY column1, column2, … ASC|DESC;

Example Database

Below is the example of a database table named “details.”

Id

Name

Class

section_id

1

Alex

5

1

2

Rolex

6

2

3

Salex

7

2

4

Lelex

8

3

5

Kelex

9

5

ORDER BY Example

Example: Use the “ORDER BY” keyword to select the records from the column named “Class” from the whole table.

				
					SELECT * FROM details
ORDER BY Class;
				
			

Example: Use the “ORDER BY” keyword to select the records from the column named “Id” order by “section_id”.

				
					SELECT Id FROM details
ORDER BY section_id;

				
			

ORDER BY DESC Example

Example: Use the “DESC” keyword to select the records in descending order from the column named “Class” from the whole table.

				
					SELECT * FROM details
ORDER BY Class DESC;

				
			

ORDER BY DESC Example

Example: Use the “DESC” keyword to select the records in descending order from the column named “Class” from the whole table.

				
					SELECT Id FROM details
ORDER BY Class DESC;

				
			

ORDER BY more than one column

				
					SELECT * FROM details
ORDER BY Id, Class;
				
			

ORDER BY DESC more than one column

Example: Use the “DESC” keyword to select the records in descending order from the column named “Class” from the whole table.

				
					SELECT * FROM details
ORDER BY Class ASC, Id DESC;
				
			

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on