SQL Server > Query

Sorting on multiple columns in SQL Server

How to sort records based on multiple columns in SQL Server database?


Records can be sorted based on multiple columns values too, to do that separate the column name by comma (“,”) followed by ASC or DESC keywords.

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

Above query will give the sorted records based on Age (in ascending order as if not specified, ASC is treated by default) and then based on Active column values.

Important

Sorting can be done on any type of data (string, integer, double, date etc.) columns, its SQL Server responsibility to sort the record based on data type’s values and give us the correct result.




 Views: 9104 | Post Order: 47



Write for us






Hosting Recommendations