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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.