The
ORDER BY
Clause
The ORDER BY clause allows you to sort the rows in the output for presentation purposes. In terms of
logical query processing, ORDER BY is the very last clause to be processed. The sample query shown
in Listing 2-4 sorts the rows in the output by employee ID and order year.
LISTING 2-4
Query Demonstrating the ORDER BY Clause
SELECT empid, YEAR(orderdate) AS orderyear, COUNT(*) AS numorders
FROM Sales.Orders
WHERE custid = 71
GROUP BY empid, YEAR(orderdate)
HAVING COUNT(*) > 1
ORDER BY empid, orderyear;
This query generates the following output.
empid orderyear numorders
----------- ----------- -----------
1 2007 2
1 2008 3
2 2008 2
3 2007 2
4 2008 3
5 2007 3
6 2007 3
7 2008 2
8 2007 4
(9 row(s) affected)
www.it-ebooks.info
Do'stlaringiz bilan baham: |