Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Sql Mysql-example MySQL Order By

Related Tutorials


 
 

Share on Google+Share on Google+

MySQL Order By

Advertisement
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

     

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:

 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 20, 2009

Related Tutorials

Discuss: MySQL Order By  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Krupa
April 14, 2012
hi

hi.. n thanks a lot... You help me
DMCA.com