From the course: SQL Server 2014 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Sorting results with ORDER BY

Sorting results with ORDER BY - SQL Server Tutorial

From the course: SQL Server 2014 Essential Training

Start my 1-month free trial

Sorting results with ORDER BY

- SQL SELECT statements also allow us to easily sort the results based on some field from smallest to largest. So I'll open up a new query and we'll start with the basic query we've been using. Go ahead and Execute that. I can actually sort the results by any of these columns. Probably the easiest to demonstrate is the numerical VacationHours column. So at the end of the SELECT statement I'm going to type in the keywords ORDER BY and there is a space between ORDER and BY. So I type in ORDER BY and now all I need to do is type in the name of the column that I would like to sort on. In this case, VacationHours. Now I'll Execute that. And now we see the first results that came up or some employees that have zero vacation hours. And we can scroll all the way down to the end of the result set and we see the people with the most vacation hours come up at the end. So the default sort behavior is to go from the smallest value to the largest value. If you would like to accomplish the opposite…

Contents