Home » SQL

Sort resultSet using ORDER BY clause in SQL



In this article, we are going to learn about the process of sorting data using ORDER BY clause in SQL.
Submitted by Manu Jemini, on March 06, 2018

If you are using SQL, you will somehow need this to arrange your result in a particular manner.

This is very useful when we want don’t want work in our server or front end to arrange data. Let’s say you have a table of registration of students in a college.

Every registered record will have an id or a date on which it was created. Having a created date is very useful at these moments but an auto generated id will do the trick too.

All we need to do is to select the data in whichever way we want and in the end just use order by keyword and give the fieldname with which we want to use the order by key word.

There is an optional keyword for sorting the data in ascending or descending order. So to say for the situation above we will need a descending data.

Dummy data:

Sort resultSet using ORDER BY clause in SQL

Syntax 1:

SELECT c-list 
FROM t 
[WHERE condition] 
[ORDER BY c1, c2, ... c] [ASC];

Syntax 2:

SELECT c-list 
FROM t 
[WHERE condition] 
[ORDER BY c1, c2, ... c] [DESC];

Example 1:

SELECT * FROM 'includehelp'.'employee' 
ORDER BY NAME, SALARY;
Sort resultSet using ORDER BY clause in SQL 1

Example 2:

SELECT * FROM 'includehelp'.'employee' 
ORDER BY NAME DESC;
Sort resultSet using ORDER BY clause in SQL 2

Example 3:

SELECT * FROM 'includehelp'.'employee' 
ORDER BY SALARY ASC;
Sort resultSet using ORDER BY clause in SQL 3






Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions





© https://www.includehelp.com (2015-2018), Some rights reserved.




close Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.