Order by Clause in SQL Query

Use of Order by Clause in SQL Query

Order by clause is used for sorting the data for specific column wise sequence. You can specify multiple column for sorting the output from the select query.

Syntax

Select * from table_name order by columnname [asc|desc], colname2 [asc|desc];
Note: ASCENDING (ASC) is default one if you don't specify anything

Example
Sort in DESCENDING: I want to sort and see the highest salary employee on the top of my employee tables.

Select * from employee order by salary desc;

Sort in ASCENDING Order: I want to see lowest salary employee on the first one.

select * from employee order by salary;

Sort Multiple columns: I want to sort department wise highest salary on top.

Select * from employees order by dept_no asc, salary desc;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.