Lesson 6. Sorting Data

You sort data by using the ORDER BY clause.

Data can be ordered in ascending order or descending order by using ASC or DESC respectively in your ORDER BY clause.

You can order the data using as many columns as you like. You can even specify ascending or descending on each column.

Examples

One Column

In [ ]:
USE AdventureWorks2016

SELECT FirstName, LastName
FROM Person.Person
ORDER BY LastName

More Than One Column

In [ ]:
USE AdventureWorks2016

SELECT FirstName, LastName
FROM Person.Person
ORDER BY FirstName, LastName

One Column Descending And Ascending

In [ ]:
USE AdventureWorks2016

SELECT FirstName, LastName
FROM Person.Person
ORDER BY LastName ASC

SELECT FirstName, LastName
FROM Person.Person
ORDER BY LastName DESC

Multi-Column With Mixed Ordering

In [ ]:
USE AdventureWorks2016

SELECT FirstName, LastName
FROM Person.Person
ORDER BY FirstName ASC, LastName DESC

Copyright © 2020, Mass Street Analytics, LLC. All Rights Reserved.