Friday, September 11, 2020

HOW TO USE ORDER BY IN SQL SERVER

Order By: When we execute select statement to fetch the records or result sets from table in SQL Server then all records doesn't come in a sorting order. So at that time, we use Order By clause to sort the records or result sets in ascending and descending order.

The Order By clause is used into the SELECT Statement or SELECT Query.

The Order By clause is used after the WHERE Condition in SELECT Statement or SELECT Query.

Sometimes, the records or result sets fetch from table by default in ascending order. 

We can sort records or result sets in ascending and descending order by using Order By ASC|DESC.

Order By ASC: The Order By ASC clause is used to sort the records or result sets in ascending order. The syntax is as below:

SELECT * FROM TABLE WHERE CONDITION ORDER BY COLUMN

                           OR

SELECT * FROM TABLE WHERE CONDITION ORDER BY COLUMN ASC

Here, we have an EMPLOYEE table and we will apply sorting in ascending order into the select statement. Please see the below screenshot for further clarification.


Order By DESC: The Order By DESC clause is used to sort the records or result sets in descending order. The syntax is as below:

SELECT * FROM TABLE WHERE CONDITION ORDER BY COLUMN DESC

Here, we have an EMPLOYEE table and we will apply sorting in descending order into the select statement. Please see the below screenshot for further clarification.


                 - - - - - - Cheers, Happy to Help! - - - - - - 

 

1 comment:

DIFFERENCE BETWEEN CHAR AND VARCHAR IN SQL SERVER

CHAR:  The  CHAR datatype is a fixed length data type in sql server. It is used to store fixed length type of string data or character strin...