Database Articles

  Home arrow Database Articles arrow Page 2 - Clauses, Sorting, and SQL Queries
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 / 5
    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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    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

    blog comments powered by Disqus

    DATABASE ARTICLES ARTICLES

    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual
    - Using Oracle SQL Developer with SQL Statemen...
    - Fixing Errors with Oracle SQL Developer


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap