Database Articles

  Home arrow Database Articles arrow Page 2 - Power Selecting
DATABASE ARTICLES

Power Selecting
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2004-12-20

    Table of Contents:
  • Power Selecting
  • Order By
  • Distinct
  • As
  • Like
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Power Selecting - Order By


    (Page 2 of 6 )

    The results of a query may not necessarily be returned in the desired order. Consider a query to return the first and last names from the Clients table.

    SELECT LastName, FirstName from Clients;

    LastName FirstName
    ------------------
    LaBelle  Nathan
    Adams    Gwendolyn
    Smith    Harold
    Chavez   Jose
    Adams    Joseph
    Copland  Dorothy
    Johnson  Diane
    Smith    Aaron
    Franklin Susanna
    Wilson   Jessica
    Brown    Michael
    Smith    Josh

    The results from a query statement are returned in a seemingly arbitrary order.

    One of the main advantages of a database server is its ability to sort large amounts of information quickly, so it makes sense to have the database sort the results instead of scripting a sort routine to do so. ORDER BY can be used to return sorted results.

    ORDER BY accepts the columns that will act as sort indexes and then ASC for an ascending sort order or DESC for a descending sort order.

    SELECT LastName, FirstName from Clients ORDER BY LastName ASC;

    LastName FirstName
    ------------------
    Adams    Gwendolyn
    Adams    Joseph
    Brown    Michael
    Chavez   Jose
    Copland  Dorothy
    Franklin Susanna
    Johnson  Diane
    LaBelle  Nathan
    Smith    Harold
    Smith    Aaron
    Smith    Josh
    Wilson   Jessica

    By specifying an ascending sort order based on the LastName column, results are returned alphabetically based on the client's last name.

    Notice, however, a closer look reveals the entire results set is not completely alphabetical (as illustrated by the three Smiths, Harold, Aaron and Josh). Multiple sorting criteria may be specified.

    SELECT LastName, FirstName from Clients ORDER BY LastName ASC, FirstName ASC;

    LastName FirstName
    ------------------
    Adams    Gwendolyn
    Adams    Joseph
    Brown    Michael
    Chavez   Jose
    Copland  Dorothy
    Franklin Susanna
    Johnson  Diane
    LaBelle  Nathan
    Smith    Aaron
    Smith    Harold
    Smith    Josh
    Wilson   Jessica

    More Database Articles Articles
    More By bluephoenix

    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 6 - Follow our Sitemap