MySQL Order By

Here, you will learn how to use MySQL Order By clause. The Order By clause is used for sorting the data either ascending or descending order according to user requirement.

MySQL Order By

MySQL Order By

     

Here, you will learn how to use MySQL Order By clause. The Order By clause is used for sorting the data either ascending or descending order according to user requirement.

ORDER BY: This keyword is used to sort the records in ascending order by default. If user wants to sort the records in descending order, they should use the DESC keyword.

 

 

 

 

Table: employee

CREATE TABLE `employee` ( 
`emp_id` int(11) NOT NULL auto_increment, 
`emp_name` varchar(10) character set utf8 NOT NULL, 
`emp_salary` int(11) NOT NULL, 
`emp_startDate` datetime NOT NULL, 
`dep_name` varchar(50) NOT NULL, 
PRIMARY KEY (`emp_id`) 
)

Use Order By in a sql query: (Ascending Order)

SELECT * FROM employee ORDER BY (emp_salary);

It will show the result set in the ascending order of emp_salary column data.

Output:

The following query is same as above query. Mentioning the asc keyword is not necessary because it is by default order.

SELECT * FROM employee ORDER BY(emp_salary) asc;

Output:

Use Order By in a sql query: (Descending Order)

SELECT * FROM employee ORDER BY(emp_salary) desc;

It will show the result set in the descending order of emp_salary column data.

Output: