BrainDump
  Home arrow BrainDump arrow Beginning SQL the SEQUEL: Working with...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Developerworks
 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? 
BRAINDUMP

Beginning SQL the SEQUEL: Working with Advanced SQL Statements
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 11
    2007-09-26

    Table of Contents:
  • Beginning SQL the SEQUEL: Working with Advanced SQL Statements
  • AND and OR...Or OR and And
  • The IN-credible Statement
  • NOT BETWEEN

  • 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

    Anyone looking for a way to modernize legacy data or easily migrate to a more cost-effective database without sacrificing functionality will benefit from this seminar. View the Intro to Advantage Database Server now!

    Beginning SQL the SEQUEL: Working with Advanced SQL Statements
    (Page 1 of 4 )

    So you made it through the first tutorial and now you are back for more. Well buckle up for the ride, because this episode teaches you the glories of advanced SQL statements. By the end of this article you'll be able to sort data, join data, you name it.

    That's right, hordes of rows and columns will be at your mercy. You won't only know SQL -- you'll be SQL. So slap on your cool shades and trench coat and get ready to dodge bullets in slow motion. And prepare to become the one.

    Disclaimer: You won't actually be able to dodge bullets after reading this article. You will however be able to avoid being shot at by your boss when you show him your l33t programming skillz.

    Sorting Things Out

    Before I became a big, illustrious writer with oodles of fans, I worked for a boss who liked to sort everything. Even if it couldn't be sorted, he wanted me to sort it. Hours on end he would call me into his office imploring me to sort by name, date, value. And off I would scurry to do his bidding. Then one day he began to ask me to sort things around his office. Like his garbage can and the stapler. Last I heard he was in a small room with a white jacket. They say when the nurses come to bring him his pills, he whispers softly, "Can you sort those for me?"

    To sort in SQL, you will need a handy little phrase called the ORDER BY statement.

    SELECT FirstName, LastName from EMPLOYEES

    ORDER BY LastName;

    In the above code, we are telling the database to choose and display the columns FirstName and LastName from our table, Employees, and to then sort them alphabetically by the last name.

    If you wanted to sort the same two columns in reverse alphabetical order, then you would do so by adding DESC (descending) to the code.

    SELECT FirstName, LastName from EMPLOYEES

    ORDER BY LastName DESC;

    You can, of course, also sort by more than one column. Say you wanted the first name, last name, and salary of every employee, sorted first by last name, then by salary.

    SELECT FirstName, LastName, SALARY from EMPLOYEES

    ORDER BY LastName, SALARY;

    And finally, for a last bit of sorting madness, if you wanted to sort by LastName in Ascending order and SALARY in Descending order, you would grab a safety helmet and type in the following code:

    SELECT FirstName, LastName, SALARY from EMPLOYEES

    ORDER BY LastName ASC, SALARY DESC;

    More BrainDump Articles
    More By James Payne


       · This is the second SQL article in my series of Beginning SQL. It covers working with...
       · Even though I use some SQL, these articles give an easy to read overview and...
       · Nice to know that learning SQL, can be fun , literally.
       · Thanks for the kind words; I'm glad you guys enjoyed it.
     

       

    BRAINDUMP ARTICLES

    - Outsourcing: the Hoopla, the Reality
    - MySQL Plays in the Sun
    - All About SQL Functions
    - SQL: Functioning in the Real World
    - More Advanced SQL Statements
    - Beginning SQL the SEQUEL: Working with Advan...
    - Beginning SQL
    - A Look at the VI Editor
    - A Quick Tour of Boo
    - Book Review: Open Source Licensing
    - PGP and GPG: Email for the Practical Parano...
    - Microsoft Continues War on Open Source
    - Secure Remote Desktop Sharing with VNC on Li...
    - A Look at Google Project Hosting
    - What we can Learn from Two Linux vs. Microso...




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