BrainDump
  Home arrow BrainDump arrow Page 3 - SQL: Functioning in the Real World
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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

SQL: Functioning in the Real World
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2007-12-10

    Table of Contents:
  • SQL: Functioning in the Real World
  • What You Talkin Bout Willis?
  • Count
  • Count Distinct
  • Minimum and Maximum

  • 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


    SQL: Functioning in the Real World - Count


    (Page 3 of 5 )

    Remember that crazy vampire from Sesame Street with the obsessive compulsive disorder? Always counting everything. One frightened child, two frightened children, AH AH AH!

    SQL has its own Count, though this one isn't likely to lure your child into an alleyway and drain all its blood. It counts every row that does not have a value of Null and returns the value. Before we see what that looks like in code, let's add a new value to our table. Our new employee is going to have the Name Go-Go Roboto, have no listed age, and make $100,000. When you are done, you table should look like this:


    Name

    Age

    Salary

    Ang Ree

    92

    $12,000

    Godzilla Nokamura

    45

    $50,000

    Mothra Fujiyama

    27

    $80,000

    King Kong Wong

    19

    $100,000

    Go-Go Roboto


    $100,000

    As part of the upcoming lawsuit, you might want to check to make sure that every employee in the database has an age listed. To do that, you could use the Count function:

      Select Count(Age) from Employees

    This would return the result:

      4

    That is because four fields contain data, and the fifth contains a Null value (or no value), and thus is not counted.

    Of course, knowing the number of rows with good data is useless unless we know the number of total rows. To get this result, we would use the Count(*) function.

      Select Count(*) From Employees

    The result in this case would be:

      5

    Now that we have both the number of rows in our table, and the number of rows missing data from our Age column, we can do the math and know that one row has no data.

    But we're not through with Counting yet! Remember the Count is OCD, and so are we. Let's fix the database by adding Go-Go Roboto's age, which is 29.

     

    Name

    Age

    Salary

    Ang Ree

    92

    $12,000

    Godzilla Nokamura

    45

    $50,000

    Mothra Fujiyama

    27

    $80,000

    King Kong Wong

    19

    $100,000

    Go-Go Roboto

    29

    $100,000

    The next step in our lawsuit will be to find out how many young whipper snappers work for the company. We can also use the Count(*) function for this:

      Select Count(*) from Employees Where Age < 30

    This will show us all of the employees aged 29 or less, which in this case, is 3.

    Average age is 45, average salary for younger employees is way higher, and there are more youthful employees than old-timers...look out frivolous lawsuit, here we come.

    More BrainDump Articles
    More By James Payne


       · Thanks for dropping by to read my article on SQL Functions. Here we discuss such...
     

       

    BRAINDUMP ARTICLES

    - More Amazing Things to Do With Pipelines
    - Pipelines Can Do Amazing Things
    - Better Command Execution with bash
    - Executing Commands with bash
    - 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...





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