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:
Practice SQL Exercises
- SQL Exercises, Practice, Solution
- SQL Retrieve data from tables [33 Exercises]
- SQL Boolean and Relational operators [12 Exercises]
- SQL Wildcard and Special operators [22 Exercises]
- SQL Aggregate Functions [25 Exercises]
- SQL Formatting query output [10 Exercises]
- SQL Quering on Multiple Tables [8 Exercises]
- FILTERING and SORTING on HR Database [38 Exercises]
- SQL JOINS
- SQL SUBQUERIES
- SQL Union[9 Exercises]
- SQL View[16 Exercises]
- SQL User Account Management [16 Exercise]
- Movie Database
- BASIC queries on movie Database [10 Exercises]
- SUBQUERIES on movie Database [16 Exercises]
- JOINS on movie Database [24 Exercises]
- Soccer Database
- Introduction
- BASIC queries on soccer Database [29 Exercises]
- SUBQUERIES on soccer Database [33 Exercises]
- Hospital Database
- Employee Database
- More to come!
Want to improve the above article? Contribute your Notes/Comments/Examples through Disqus.
Previous: SQL character function TRANSLATE
Next: Group By
SQL: Tips of the Day
SQL Server SELECT into existing table.
INSERT INTO dbo.TABLETWO SELECT col1, col2 FROM dbo.TABLEONE WHERE col3 LIKE @search_key
This assumes there's only two columns in dbo.TABLETWO - you need to specify the columns otherwise:
INSERT INTO dbo.TABLETWO (col1, col2) SELECT col1, col2 FROM dbo.TABLEONE WHERE col3 LIKE @search_key
Database: SQL Server
Ref: https://bit.ly/3y6tpA3
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework