BrainDump
  Home arrow BrainDump arrow Page 2 - 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 
 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

    Ziff Davis Enterprise Virtual Tradeshows: Hot Topics, Cutting Edge Technology, Real-time Interaction with IT Professionals. Learn more at ziffdavisvts.com

    Beginning SQL the SEQUEL: Working with Advanced SQL Statements - AND and OR...Or OR and And
    (Page 2 of 4 )

    If it's any comfort, that title hurt my head typing it as much as it did yours reading it. In the previous tutorial we spoke about the WHERE CLAUSE and its uses. Here, we will go a little more in-depth and add some power to it using the AND and OR operators.

    AND

    Say you wanted to list all the people in our employee database that had the first name Homer and the last name Sampson (if you remember in the first tutorial I had you add Homer's twin brother, Homer, to the table). One way we could do this is by joining the WHERE clause with the AND operator (think Voltron with words).

    SELECT * from EMPLOYEES

    WHERE FirstName='Homer'

    AND LastName='Sampson';

    The above sample would return the following dataset:

    First Name

    Last Name

    Position

    Salary

    Homer

    Sampson

    CEO

    $75,000

    Homer

    Sampson

    CFO

    $75,000

    So let's say that Homer Sampson has a daughter, we'll call her, I don't know...Liza (to avoid a lawsuit from Matt Groening and Sam Simon). She isn't married yet because she is a high-powered business woman, devoted to her job, so her last name is still Sampson. Her salary is a little more than her father's, because she is the CTO and daddy's little baby. So let's give her $85,000 a year. Go ahead and add her to the employee table.

    OR

    Now that we have added Liza Sampson to our employee table, let's do some more coding. In this example, we want to list all people with either the first name Liza, or the last name of Sampson.

    SELECT * from EMPLOYEES

    WHERE FirstName="Liza'

    OR LastName='Sampson";

    Here's how that should look in your table:

    First Name

    Last Name

    Position

    Salary

    Homer

    Sampson

    CEO

    $75,000

    Homer

    Sampson

    CFO

    $75,000

    Liza

    Sampson

    CTO

    $85,000

    Lastly, you can combine the might of the AND and OR statements. I know -- how can one man wield that much power? Well you can -- and will -- with the following code.

    SELECT * from EMPLOYEES WHERE

    (FirstName='Liza' OR FirstName='Homer')

    AND LastName='Sampson';

    This code will return the same results as above.

    First Name

    Last Name

    Position

    Salary

    Homer

    Sampson

    CEO

    $75,000

    Homer

    Sampson

    CFO

    $75,000

    Liza

    Sampson

    CTO

    $85,000

    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 4 hosted by Hostway