SQL Order By

ORDER BY clause or keyword is used to sort the data in ascending or descending order.

The syntax of ORDER BY is:

SELECTcolumn_name
FROM tables
WHERE conditions
ORDER BYcolumn_name[ASC|DESC];

 

Example:

Let us take an example of employee table

Employee table:

ID Name Age Salary
1 Bhargav 32 35000
2 Varun 30 32000
3 Nithin 38 42000
4 Thakur 42 48000
5 Pranav 25 25000

 

SELECT * FROM Employee ORDER BY Salary;

 

The output displayed will be in ascending order of salary

ID Name Age Salary
5 Pranav 25 25000
2 Varun 30 32000
1 Bhargav 32 35000
3 Nithin 38 42000
4 Thakur 42 48000

 

You can also sort the data in descending order by using DESC keyword after ORDER BY clause.

If you want to sort data randomly then it is done as follows

SELECT * FROM tables
ORDER BY RANDOM()
LIMIT
1;