Skip to main content

SQL ORDER BY Clause

Body

The ORDER BY clause is used in the SQL SELECT statement to order the data in ascending or descending order, this may help us get a better look when analyzing data, the ORDER BY clause can sort numeric as well as alphabetical data in both ascending and descending order.

SQL ORDER BY Clause Syntax

SELECT column_name(s) FROM table_name ORDER BY column ASC|DESC

ASC as ascending order
DESC as descending order

Example:

SELECT * FROM Dept ORDER BY Sal

OR

SELECT * FROM Dept ORDER BY Sal ASC

Dept:

Firstname Sal Deptno
Henry 1100 2
Scott 1300 2
Bill 1500 1
Mathew 2000 1
Steve 2500 1

If we omit the ascending and descending option the ORDER BY clause by default arranges the data in ascending order.

The following is an example of descending order.

SELECT * FROM Dept ORDER BY Sal DESC
Firstname Sal Deptno
Steve 2500 1
Mathew 2000 1
Bill 1500 1
Scott 1300 2
Henry 1100 2

Sort column by Firstname

SELECT * FROM Dept ORDER BY Firstname ASC
Firstname Sal Deptno
Bill 1500 1
Henry 1100 2
Mathew 2000 1
Scott 1300 2
Steve 2500 1

Comments

Add new comment