Monday 18 June 2012

Lesson 8: Ordering Rows in SQL


Data is stored in Scalable SQL in no particular sequence. If you want to see your data displayed in sequence, you must add an Order By clause to your Select statement.


Syntax:
Select */fieldname<mask> ...
from tablename
order by fieldname;


Explanation:
  • The Order By clause tells SQL you want the specified fields displayed in ascending order (ordered from A to Z, 1 to 100).
Example:Retrieve the city, vendor Id, and name from the TrnVendor table. Order your data by city:
  1. In the SQL text box type:
Select City, VendId, Name
from TrnVendor
order by City; 
  1. Execute the SQL statement.
Results:
 
CityVendIdName
BayshoreTV019Bayshore Consulting
BurlingtonTV020BayCon Group
ChicagoTV003BedMakers Linen
ChicagoTV016Music Maker
ChicagoTV028Distant Horizons
ChicagoTV029Paltry Play
ChicagoTV030Make Shift Tilts
DallasTV021Cooperative Operatives
DallasTV026Clampett Oil
DetroitTV005Ray Block

No comments:

Post a Comment