From the course: Querying Microsoft SQL Server 2019

Unlock the full course today

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

Sort records with ORDER BY

Sort records with ORDER BY - SQL Server Tutorial

From the course: Querying Microsoft SQL Server 2019

Start my 1-month free trial

Sort records with ORDER BY

- [Instructor] The next clause that we can add to a select statement will help you sort the records into an order that's the most useful for your needs. When I execute the query SELECT star FROM HumanResources.Department, it returns the records in an order that they're saved into the table. This is most commonly an ascending order based off of the primary key column. In this case, it's the department ID. But what if you wanted to see the records clustered together based off of the group name values instead? That way all of the research and development departments appear next to each other, and so on down the list. To do that, we can add an order by clause to the query. I'll come up here, and right before the terminator character, I'll type in ORDER BY. Now, if your query also has a where clause to filter the records, make sure that order by comes after it. So you would have select, from, where, and then order by.…

Contents