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 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
Select firstName, lastNmae, DOB, Mobile, Address from tbStudent Order by firstName DESC
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;
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
You can add multiple columns in sql query order by clause
Select * from [dbo].[tbSoldProduct] order by ProductName, SDate, UnitPrice
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.
select * from [dbo].[tbSoldProduct] order by SDate ascsql order by date newest first
select * from [dbo].[tbSoldProduct] order by SDate desc