Free Web Development Online Tutorials, Learn to Code
Learn SQL Database Development, MS SQL Tutorial Online

Order by Clause in SQL Server Database

Order by, order by desc in SQL

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.

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

Order by Clause in Descending Order Syntax

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

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;
Comment
Name
Email
Website
Subscribe
 
Order by query in SQL Database