Tutorials Hut

Tutorials Hut




  • SQL Sorting (SQL Order By)

    In SQL Sorting (SQL Order by ) ,The rows in the result set are sorted in ascending or descending order with the use of ORDER BY clause

    Syntax:

    ORDER BY [Col1 , Col2…. ]/ [Col no1 , Col no2…….] [ ASC | DESC]

    The order is ASCENDING  by Default

    Example:

    Consider Employee table having below records.

    SQL Order by Employee

    List the Employee’s  EmployeeID and name, in the increasing order of the ID

    SELECT EmployeeID, Employee_FirstName,Employee_LastName FROM Employee
    ORDER BY EmployeeID;

    OR

    SELECT EmployeeID, Employee_FirstName,Employee_LastName FROM Employee
    ORDER BY 1;
    Sql Sorting Order by 1.2

    List the Employee’s  EmployeeID and name, in the decreasing order of the ID

    SELECT EmployeeID, Employee_FirstName,Employee_LastName FROM Employee
    ORDER BY EmployeeID DESC;

    OR

    SELECT EmployeeID, Employee_FirstName,Employee_LastName FROM Employee
    ORDER BY 1 DESC;
    Sql Sorting Order by 1.3

    List the Employees and their EmployeeIDs  in the decreasing order of the Employee Last Name and increasing order of EmployeeID.

    SELECT EmployeeID, Employee_FirstName,Employee_LastName FROM Employee
    ORDER BY Employee_LastName DESC, EmployeeID;

    OR

    SELECT EmployeeID, Employee_FirstName,Employee_LastName FROM Employee
    ORDER BY 2 DESC, 1;
    Sql Sorting Order by 1.4

















  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    Scroll to Top