SQL - Order By
The order by statement allows for table column assortment. It allows for ascending or descending lists of your table column values permitting SQL to reorder your table rows for the purpose of viewing.
SQL Code:
SELECT * FROM employees ORDER BY Lastname;
SQL Table:
| ID | Lastname | Firstname | Title |
| 11 | Davis | Julie | manager |
| 10 | Harris | Joel | crew |
| 9 | Hicks | Freddy | crew |
| 2 | Hively | Jessica | crew |
| 1 | Johnson | David | crew |
The above example resorts our rows of data alphabetically by lastname. Here is another example ordering by two different columns First we alphabatize our job titles and again we order by lastname.
SQL Code:
SELECT * FROM employees ORDER BY Title,Lastname;
SQL Table:
| ID | Lastname | Firstname | Title |
| 10 | Harris | Joel | crew |
| 9 | Hicks | Freddy | crew |
| 2 | Hively | Jessica | crew |
| 1 | Johnson | David | crew |
| 11 | Davis | Julie | manager |
Hint: It is possible to order by a column you do not wish to display such as an ID field. Also notice at the bottom of each query display will be the number of rows affected by your query. This number will also be displayed anytime an insert, select, update, or delete statement is properly executed by your SQL program.
|