Order By Clause


☞ Following are the topics to be covered :

1. ORDER BY clause.
2. Fetching records in ascending order.
3. Fetching records in descending order.
4. Applying ascending and descending on multiple columns simultaneously.

ORDER BY Clause

☞ To sort the data in ascending or descending order, we use the ORDER BY clause.

☞ By default, it will sort the data in ascending order.


Fetching records in ascending order

☞ To fetch records in ascending order, we use the ORDER BY clause.

Syntax :

SELECT * FROM <table_name>
ORDER BY <column_name>;

OR

SELECT * FROM <table_name>
ORDER BY <column_name>ASC;

Example :

SELECT * FROM Student
ORDER BY name;

OR

SELECT * FROM Student
ORDER BY nameASC;

Fetching records in descending order

☞ To fetch records in descending order, we use the ORDER BY - DESC command.

Syntax :

SELECT * FROM <table_name>
ORDER BY <column_name>DESC;

Example :

SELECT * FROM Student
ORDER BY nameDESC;

Applying ascending and descending on multiple columns simultaneously

Syntax :

SELECT * FROM <table_name>
ORDER BY <column_name1>ASC, <column_name2> DESC;

Example :

SELECT * FROM Student
ORDER BY feesASC, nameDESC;