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:

IDNameAgeSalary
1Bhargav3235000
2Varun3032000
3Nithin3842000
4Thakur4248000
5Pranav2525000

 

SELECT * FROM Employee ORDER BY Salary;

 

The output displayed will be in ascending order of salary

IDNameAgeSalary
5Pranav2525000
2Varun3032000
1Bhargav3235000
3Nithin3842000
4Thakur4248000

 

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;