Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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

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 Expression: SQL ORDER BY clause - Sorting on column names.

Relational Algebra Tree:

Relational Algebra Tree: SQL ORDER BY clause - Sorting on column names.

Sample Output:

order-by


Pictorial Presentation: SQL ORDER BY ascending - descending

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 Expression: SQL - Usage of the ORDER BY clause with the DESC argument.

Relational Algebra Tree:

Relational Algebra Tree: SQL - Usage of the ORDER BY clause with the DESC argument.

Sample Output:

order by descending

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 Expression: SQL - Ordering by more than one columns.

Relational Algebra Tree:

Relational Algebra Tree: SQL - Ordering by more than one columns.

Sample Output:

order bY in more columns

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 Expression: SQL - Ordering by more than one columns in ascending or descending order.

Relational Algebra Tree:

Relational Algebra Tree: SQL - Ordering by more than one columns in ascending or descending order.

Sample Output:

SQL ORDER BY ACCENDING OR-DECENDING

See our Model Database

Practice SQL Exercises

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