Database Articles
  Home arrow Database Articles arrow Page 2 - 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 
Download TestComplete 
JMSL Numerical Library 
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


    Clauses, Sorting, and SQL Queries - The order by Clause


    (Page 2 of 5 )

    In general, the rows in a result set returned from a query are not in any particular order. If you want to your result set in a particular order, you will need to instruct the server to sort the results using the order by clause:

    Theorder byclause is the mechanism for sorting your result set using either raw column data or expressions based on column data.

    For example, here’s another look at an earlier query against theaccount table:

      mysql> SELECT open_emp_id, product_cd
          -> FROM account;

      +-------------+------------+
      | open_emp_id | product_cd |
      +-------------+------------+
     
    |          10 | CHK        |
      |          10 | SAV        |
      |          10 | CD         |
      |          10 | CHK        |
      |          10 | SAV        |
      |          13 | CHK        |
      |          13 | MM         |
      |           1 | CHK        |
      |           1 | SAV        |
      |           1 | MM         |
      |          16 | CHK        |
      |           1 | CHK        |
      |           1 | CD         |
      |          10 | CD         |
      |          16 | CHK        |
      |          16 | SAV        |
      |           1 | CHK        |
      |           1 | MM         |
      |           1 | CD         |
      |          16 | CHK        |
      |          16 | BUS        |
      |          10 | BUS        |
     
    |          16 | CHK        |
      |          13 | SBL        |
      +-------------+------------+
     
    24 rows in set (0.00 sec)

    If you are trying to analyze data for each employee, it would be helpful to sort the results by theopen_emp_idcolumn; to do so, simply add this column to theorder byclause:

      mysql> SELECT open_emp_id, product_cd
          -> FROM account
         
    -> ORDER BY open_emp_id;

      +-------------+------------+
      | open_emp_id | product_cd |
      +-------------+------------+
      |           1 | CHK        |
      |           1 | SAV        |
      |           1 | MM         |
      |           1 | CHK        |
      |           1 | CD         |
      |           1 | CHK        |
      |           1 | MM         |
      |           1 | CD         |
      |          10 | CHK        |
      |          10 | SAV        |
      |          10 | CD         |
      |          10 | CHK        |
      |          10 | SAV        |
      |          10 | CD         |
      |          10 | BUS        | 
      |          13 | CHK        |
      |          13 | MM         |
      |          13 | SBL        |
      |          16 | CHK        |
      |          16 | CHK        |
      |          16 | SAV        |
      |          16 | CHK        |
      |          16 | BUS        |
      |          16 | CHK        |
     
    +-------------+------------+
     
    24 rows in set (0.00 sec)

    It is now easier to see what types of accounts were opened by each employee. However, it might be even better if you could ensure that the account types were shown in the same order for each distinct employee; this can be accomplished by adding theproduct_cdcolumn after theopen_emp_idcolumn in theorder byclause:

      mysql> SELECT open_emp_id, product_cd
          -> FROM account
         
    -> ORDER BY open_emp_id, product_cd;

      +-------------+------------+
      | open_emp_id | product_cd |
      +-------------+------------+
      |           1 | CD         |
      |           1 | CD         |
      |           1 | CHK        |
      |           1 | CHK        |
      |           1 | CHK        |
      |           1 | MM         |
      |           1 | MM         |
      |           1 | SAV        |
      |          10 | BUS        |
      |          10 | CD         |
      |          10 | CD         |
      |          10 | CHK        |
      |          10 | CHK        |
      |          10 | SAV        |
      |          10 | SAV        |
      |          13 | CHK        |
      |          13 | MM         |
      |          13 | SBL        |
      |          16 | BUS        |
      |          16 | CHK        |
      |          16 | CHK        |
      |          16 | CHK        |
      |          16 | CHK        |
      |          16 | SAV        |
      +-------------+------------+ 
      24 rows in set (0.00 sec)

    The result set has now been sorted first by employee ID and then secondly by the account type. The order that columns appear in yourorder byclause does make a difference.

    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 4 hosted by Hostway
    Stay green...Green IT