SunQuest
 
       Database Articles
  Home arrow Database Articles arrow Page 3 - Clauses, Sorting, and SQL Queries
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE ARTICLES

Clauses, Sorting, and SQL Queries
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2007-11-08

    Table of Contents:
  • Clauses, Sorting, and SQL Queries
  • The order by Clause
  • Ascending Versus Descending Sort Order
  • Sorting via Expressions
  • Sorting via Numeric Placeholders

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Clauses, Sorting, and SQL Queries - Ascending Versus Descending Sort Order


    (Page 3 of 5 )

    When sorting, you have the option of specifying ascending or descending order via the asc and desc keywords. The default is ascending, so you will only need to add thedesckeyword if you want to use a descending sort. For example, the following query lists all accounts sorted by available balance with the highest balance listed at the top:

        mysql> SELECT account_id, product_cd, open_date, avail_balance
           
    -> FROM account
           
    -> ORDER BY avail_balance DESC;

     

     

    account_id  product_cd  open_date

     avail_balance 

     

     

    24  SBL

    2004-02-22

    50000.00

     

    23  CHK

     2003-07-30 

    38552.05 

     

    20 CHK

     2002-09-30 

    23575.12 

     

    13  CD

     2004-12-28

    10000.00

     

    22  BUS

     2004-03-22 

    9345.55

     

    18  MM

     2004-10-28

    9345.55

     

    10  MM

     2004-09-30

    5487.09 

     

    14 CD

     2004-01-12 

    5000.00 

     

    15  CHK

     2001-05-23 

    3487.19 

     

     

    3  CD

     2004-06-30

    3000.00 

     

    4  CHK

    2001-03-12

    2258.02

     

    11  CHK

     2004-01-27 

    2237.97

     

    7  MM

     2002-12-15

    2212.50

     

    19  CD

     2004-06-30 

    1500.00

     

    1  CHK

     2000-01-15 

    1057.75 

     

    6  CHK

     2002-11-23

    1057.75 

     

    9  SAV

    2000-01-15 

    767.77 

     

    8 CHK

     2003-09-12 

    534.12 

     

    2  SAV

     2000-01-15

    500.00

     

    16  SAV

     2001-05-23 

    387.99 

     

    5 SAV

     2001-03-12

    200.00 

     

    17  CHK

    2003-07-30

    125.67 

     

    12 CHK

     2002-08-24

    122.37 

     

    21  BUS

     2002-10-01

    0.00 

     

    24 rows in set (0.01 sec)

    Descending sorts are commonly used for ranking queries, such as “show me the top 5 account balances.” MySQL includes alimitclause that allows you to sort your data and then discard all but the first X rows; see Appendix B for a discussion of thelimitclause, along with other non-ANSI extensions.

    More Database Articles Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Learning SQL," published by O'Reilly. We...
     

    Buy this book now. This article is excerpted from chapter three of the book Learning SQL, written by Alan Beaulieu (O'Reilly; ISBN: 0596007272). Check it out today at your favorite bookstore. Buy this book now.

    DATABASE ARTICLES ARTICLES

    - More on Query Optimization for Oracle Databa...
    - Query Optimization in Oracle
    - Clusters and Other Data Structures for Oracle
    - Using Indexes with an Oracle Database
    - The Basics of Data Structures in Oracle
    - Oracle Data Structures
    - Best Practices for PL/SQL Variables
    - What`s Code Without Variables?
    - Clauses, Sorting, and SQL Queries
    - The From Clause and SQL Queries
    - Query Primer
    - Full Text Searches and Strings
    - Searching with Strings
    - Pattern Matching with Strings
    - Working with Cases of Strings






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway