Contents Index Explicitly limiting the number of rows returned by a query The ROLLUP operation: adding summary information to GROUP BY queries

ASA SQL User's Guide
  Summarizing, Grouping and Sorting Query Results
    The ORDER BY clause: sorting query results

ORDER BY and GROUP BY


You can use an ORDER BY clause to order the results of a GROUP BY in a particular way.

Example 

The following query finds the average price of each product and orders the results by average price:

SELECT name, AVG(unit_price)
FROM product
GROUP BY name
ORDER BY AVG(unit_price)
name AVG(product.unit_price)
Visor 7
Baseball Cap 9.5
Tee Shirt 12.333333333
Shorts 15
... ...

Contents Index Explicitly limiting the number of rows returned by a query The ROLLUP operation: adding summary information to GROUP BY queries