For the past few Jump Start columns we’ve been looking at different ways to use the SELECT statement. As you’ve seen, the SELECT statement is really the core SQL statement and its incredible flexibility is one of the key features that give relational database systems the power they command. Most recently, we have been diving deep into subqueries – showing how you can essentially embed one SQL SELECT statement within another SELECT statement. Subqueries are a powerful tool. However, the SELECT statement has a number of other functions that are more commonly used than subqueries. In this column we’ll look at using one of the most basic and essential SELECT functions: the ORDER BY clause.
Many times when you retrieve data from a database you might want that data ordered in a variety of different ways and the order that’s returned by the raw SELECT statement might not suit your needs. The ORDER BY clause enables you to alter the order of the results that are returned by the SQL SELECT statement.

USE northwind
GO
SELECT OrderID, CustomerID, ShipName FROM Orders

Not surprisingly this query returns the data by OrderID but what if you want a listing of orders per customer? That situation is where the ORDER BY clause comes into play. The following SELECT statement shows how you can use the ORDER BY clause to get a result set that’s returned in CustomerID order.

USE northwind
GO
SELECT OrderID, CustomerID, ShipName FROM Orders
ORDER BY CustomerID