ORDER BY Clause in MySQL

Order By Clause

The ORDER BY clause in MySQL in used to order the records according to column which we choose and then display them on the screen using SELECT statement.

Syntax: SELECT requiredcolumns FROM tablename ORDERBY columname;

Example:

Suppose, there is a table named employee and we want to order it according to the salary received in ascending order, then the statement to be written is as follows :

SELECT * FROM employee ORDER BY salary ASC;

The default order in which the columns get sorted is ascending. So, if we want to display records with highest to lowest salary i.e. descending order then we need to add a word DESC after ORDER BY salary clause. The statement is as follows: 

SELECT * FROM employee ORDER BY salary DESC;

Conclusion:

Order by clause can be used among many other clauses like LIMIT, WHERE, GROUP BY, etc. and this is also used in window functions. While working on real-time examples like top customers in the database or highest scorer in school, this ORDER BY clause helps a lot for sorting records as per our requirement.

Comments