SQL Server > Query

Sort records from SQL server in SQL Server

How to sort records from the SQL Server database table?


To return sorted records from SQL Server database table, we use ORDER BY followed by column name to sort and ASC (for ascending order) and DESC (for descending order) keywords.

If ASC or DESC is not specicied, ASC is treated by default.

SELECT PersonalDetailsId, FirstName,
                    LastName, Age, Active
                    FROM PersonalDetails
                    ORDER BY Age

Above query would sort all records from the PersonalDetails table based on Age column value in ascending order. Notice that we have not mentioned ASC or DESC after ORDER BY Age column as by default its ascending order.

See following query

SELECT PersonalDetailsId, FirstName,
                    LastName, Age, Active
                    FROM PersonalDetails
                    ORDER BY Age DESC

Keeping the DESC keyword in the last, will sort  records based on Age column in descending order.

 Views: 9311 | Post Order: 46



Write for us






Hosting Recommendations