The SQL ORDER BY clause is the part of the SELECT statement that controls the sequence of information returned.
The SQL ORDER BY clause is really a sort feature that enables the output to be sorted by one or more columns.
Use the SQL ORDER BY clause whenever it is desirable to control the sequence output through a SELECT clause. It might be used to alphabetize a report or to print the most import items at the beginning of a report.
SQL ORDER BY is used as follows.
SQL ORDER BY Syntax
Multiple columns can be included in the ORDER BY clause. The direction of the sort is controlled by:
SQL ORDER BY Example
The following example displays rows from the branch in region_nbr and branch_nbr ascending sequence.
Here are the contents of the table:
Table: BRANCH
branch_nbr | branch_name | region_nbr | employee_count |
108 | New York | 100 | 10 |
110 | Boston | 100 | 6 |
212 | Chicago | 200 | 5 |
404 | San Diego | 400 | 6 |
415 | San Jose | 400 | 3 |
This SQL Statement with ORDER BY is executed:
Here is the result.
region_nbr | branch_nbr | branch_name |
100 | 108 | New York |
100 | 110 | Boston |
200 | 212 | Chicago |
400 | 404 | San Diego |
400 | 415 | San Jose |
Advertisements:
Infogoal.com is organized to help you gain mastery.
Examples may be simplified to facilitate learning.
Content is reviewed for errors but is not warranted to be 100% correct.
In order to use this site, you must read and agree to the
terms of use, privacy policy and cookie policy.
Copyright 2006-2020 by Infogoal, LLC. All Rights Reserved.