Free Web Development Online Tutorials, Learn to Code
SQL order by multiple columns, sql order by date, sql order by alphabetical, sql order by number

SQL Order by 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 asc and desc

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.

SQL order by ascending syntax
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

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
ORDER BY (CASE City
WHEN 'Mumbai' 	 THEN 1
WHEN 'Navi Mumbai' 	 THEN 2
WHEN 'Delhi' 	 THEN 3
WHEN 'New Delhi' THEN 4
WHEN 'Hyderabad/ Secunderabad' 	THEN 5
ELSE 100 END) ASC, City DESC;

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
Comment
Name
Email
Website
Subscribe
MS SQL Examples | Join MS SQL Course