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.
|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.
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;|
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.
Posted on: January 20, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles