Join Class

Order by clause in SQL Query Example

SQL order by clause example, order by clause will help you to arrange data as per your requirement, you can use order by clause with one or multiple columns.

SQL Order by ascending and descending

SQL order by clause is used for sorting the data in ascending or descending order, if we don’t specify anything then data sorted by ascending order by default.

Here is query structure of SQL order by ascending syntax in SQL database

Select Col1,Col2, Col3 from table_name order by column_name

Select firstName, lastNmae, DOB, Mobile, Address            
from tbStudent            
order by firstName

You also can use more than one column in order by statement

Select firstName, lastNmae, DOB, Mobile, Address
from tbStudent            
Order by firstName, DOB

SQL Order By Descending Example

There are various ways we can use order by descending clause in sql query.

Select firstName, lastNmae, DOB, Mobile, Address
from tbStudent            
Order by firstName DESC
SQL Order By Clause with Case Statement

You can write customized order statement with help of CASE statement, Notice order by ASC and DESC in same place

Select * from tbContactList
WHEN 'Mumbai' 	 THEN 1
WHEN 'Navi Mumbai' 	 THEN 2
WHEN 'Delhi' 	 THEN 3
WHEN 'New Delhi' THEN 4
WHEN 'Hyderabad/ Secunderabad' 	THEN 5
SQL order by asc and desc in same query

You can use sql order by both ascending (asc) and descending (desc) in same sql query.

Select * from [dbo].[tbSoldProduct]
order by ProductName asc, SDate desc
SQL order by multiple columns

You can add multiple columns in sql query order by clause

Select * from [dbo].[tbSoldProduct]
order by ProductName, SDate, UnitPrice
SQL order by date

We often use order by date clause to check data to generate report. Sometimes we need to see the oldest record first, then we add desc (descending) in order by clause, and when we want new records first we should use asc (ascending), which is by default, even if we don’t specify then also it will show the record in ascending order.

sql order by date oldest first
select * from [dbo].[tbSoldProduct]
order by  SDate asc
sql order by date newest first
select * from [dbo].[tbSoldProduct]
order by SDate desc
Hire SQL Developer
Order by query in SQL Database
SQL Training: For any group or corporate training, please contact us at webtrainingroom(at)gmail.
SQL job Interview Questions Answers
Course in Demand
SQL database development tutorials for learning sql query, data manipulation language, working with MS SQL Server.
MS SQL Examples | Join MS SQL Course