ORDER BY Clause

SELECT returns rows in an unspecified order by default. To sort returned rows in ascending or descending order, use the ORDER BY clause.

ORDER BY takes the following syntax.

This example shows how ORDER BY works with multiple fields.

Here, the SELECT statement orders by the State and Category columns. Alabama and Furniture are the first alphabetized fields for their respective columns. For all of the rows where State is equal to Alabama and Category is equal to Furniture, SQL returns the Sub-Category column values without any order. When State is equal to Alabama, and Category has displayed all of the Furniture values, the Category column displays Office Supplies, the next value in alphabetical order.

StateCategorySub-Category
AlabamaFurnitureChairs
AlabamaFurnitureChairs
AlabamaFurnitureChairs
AlabamaFurnitureChairs
AlabamaFurnitureFurnishings
AlabamaFurnitureTables
AlabamaFurnitureChairs
AlabamaFurnitureFurnishings
AlabamaFurnitureTables
AlabamaFurnitureFurnishings
AlabamaFurnitureTables
AlabamaFurnitureChairs
AlabamaOffice SuppliesAppliances

To order the columns in ascending or descending order, use the keywords ASC and DSC. The order is ascending by default. To sort fields that contain null values, specify NULLS FIRST or NULLS LAST. By default, null values are treated as the largest. In ascending order, they are ordered last. In descending order, they are first.