SQL ORDER BY clause
ORDER BY clause
The ORDER BY clause orders or sorts the result of a query according to the values in one or more specific columns. More than one columns can be ordered one within another. It depends on the user that, whether to order them in ascending or descending order. The default order is ascending.
The SQL ORDER BY clause is used with the SQL SELECT statement.
Note: SQL ORDER BY clause always come at the end of a SELECT statement.
Syntax:
SELECT <column_list> FROM < table name >. WHERE <condition> ORDER BY <columns> [ASC | DESC];
Parameters:
Name | Description |
---|---|
table_name | Name of the table. |
column_list | Name of the columns of the table. |
columns | Name of the columns which will participate in ordering. |
Syntax diagram: SELECT query
Example: SQL ORDER BY clause - Sorting on column names
Find the agent name, working area and commision; sort the result by agent code ascending order.
Sample table: agents
To get 'agent_name', 'agent_name','working_area' and 'commission' from the 'agents' table with following condition -
1. 'agent_code' should come in ascending order,
the following SQL statement can be used:
SELECT agent_code,agent_name,working_area,commission
FROM agents ORDER BY agent_code;
Relational Algebra Expression:

Relational Algebra Tree:

Sample Output:

Pictorial Presentation: SQL ORDER BY ascending - descending
Example: SQL - Usage of the ORDER BY clause with the DESC argument
To get 'agent_name', 'agent_name','working_area' and 'commission' from the 'agents' table with following condition -
1. 'agent_code' should come in descending order,
then, the following SQL statement can be used :
SELECT agent_code,agent_name,working_area,commission
FROM agents ORDER BY agent_code DESC;
Relational Algebra Expression:

Relational Algebra Tree:

Sample Output:

Example: SQL - Ordering by more than one columns
You can use the ORDER BY clause to sort the result of a query according to the values in more than one columns.
To get 'agent_name', 'agent_name','working_area' and 'commission' from the 'agents' table with following conditions -
1. 'working_area' should come in ascending order first,
2. within the same 'working_area', 'agent_code' should come in ascending order,
then, the following SQL statement can be used :
SELECT agent_code,agent_name,working_area,commission
FROM agents ORDER BY working_area,agent_code;
Relational Algebra Expression:

Relational Algebra Tree:

Sample Output:

Example: SQL - Ordering by more than one columns in ascending or descending order
You can use the ORDER BY clause to sort the result of a query on multiple columns in various order (ascending or descending). Here is an example:
To get 'agent_name', 'agent_name','working_area' and 'commission' from the 'agents' table with following conditions -
1. 'working_area' comes in ascending order first,
2. 'commission' comes in descending order,
then, the following SQL statement can be used :
SELECT agent_code,agent_name,working_area,commission
FROM agents ORDER BY working_area ASC, commission DESC;
Relational Algebra Expression:

Relational Algebra Tree:

Sample Output:

Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.
Previous: SQL character function TRANSLATE
Next: Group By
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join