SQL Order By Keyword

Friday, October 28, 2011

tags SQL

The ORDER BY keyword is used to sort the result-set by one or more specified fields (columns). The ORDER BY keyword sorts the records in ascending order by default. If you want to sort the records in a descending order, you can use the DESC keyword.


SQL ORDER BY Syntax

SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC | DESC

ORDER BY Example

In this example, we will use the SELECT statement to build a result-set that includes all fields in the Customers table ordering the results by the LastName field.


SELECT *
FROM Customers
ORDER BY LastName

Same example but using the ORDER BY keyword to display the results in descending order.

SELECT *
FROM tbl_employees
ORDER BY employeeID

Results
employeeIDemployeeName
1000John Smith
1001Fred White
1002Jane Scott
1003Samuel Williams

Did you find the page informational and useful? Share it using one of your favorite social sites.

Recommended Books & Training Resources

Head First SQL: Your Brain on SQL A Learners Guide SQL Cookbook