Web www.tizag.com

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:

IDLastnameFirstnameTitle
11DavisJuliemanager
10HarrisJoelcrew
9HicksFreddycrew
2HivelyJessicacrew
1JohnsonDavidcrew

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:

IDLastnameFirstnameTitle
10HarrisJoelcrew
9HicksFreddycrew
2HivelyJessicacrew
1JohnsonDavidcrew
11DavisJuliemanager

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.





New - Tizag.com Forums!
Recent Forum Topics: