Ordering Multiple Columns
When ordering your data, you can have multiple sort levels. For example, you can order your data by city and then by name within the city.
Syntax:
Select fieldname, fieldname, fieldname
from tablename
order by fieldname <desc>, fieldname <desc>, fieldname <desc>
Explanation:
- By default, the Order By clause orders the specified fields in ascending order.
- Typing "desc" after a field name in the Order By clause tells SQL you want the data in the specified field displayed in descending order (Z to A, 100 to 1).
- The first field name specified is the primary sort order, the second field name specified is the secondary sort order, and so on ...
Retrieve the city, name, and vendor ID from the TrnVendor table. Order your data by city and then by name within city:
- In the SQL text box, type:
Select City, Name, VendId
from TrnVendor
order by City, Name;
- Execute the SQL statement.
City | Name | VendId |
Bayshore | Bayshore Consulting | TV019 |
Burlington | BayCon Group | TV020 |
Chicago | BedMakers Linen | TV003 |
Chicago | Distant Horizons | TV028 |
Chicago | Make Shift Tilts | TV030 |
Chicago | Music Maker | TV016 |
Chicago | Paltry Play | TV029 |
Dallas | Clampett Oil | TV026 |
Dallas | Cooperative Operatives | TV021 |
Detroit | Ray Block | TV005 |
Detroit | Spot Out | TV007 |
Freeport | Enterprise Transport | TV024 |
Freeport | Food Four | TV017 |
Freeport | World Wide Learning U | TV023 |
Houston | Legal Lookup | TV022 |
Mercer | Bed Room Furniture, Inc. | TV004 |
Mercer | The Freelance | TV011 |
Monroe | Big Tree Landscaping | TV008 |
Monroe | Softer Software | TV013 |
New York | Paper People | TV012 |
Orlando | Against the Tide | TV025 |
Orlando | The Soda Factory | TV010 |
Park Ridge | Mosquito No Bite | TV006 |
Park Ridge | No Waste Disposal | TV015 |
Toledo | Counter Productive | TV014 |
Toledo | Hit the Deck | TV009 |
Toledo | Narrow Nest | TV027 |
Toledo | Wet Off Towels | TV001 |
Warren | The Games All Here | TV002 |
Youngstown | Computer Bytes | TV018 |
Table of Contents |