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

USE AdventureWorks2016

SELECT FirstName, LastName
FROM Person.Person
ORDER BY LastName

More Than One Column

USE AdventureWorks2016

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

One Column Descending And Ascending

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

USE AdventureWorks2016

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

Last updated