BrainDump
  Home arrow BrainDump arrow Page 2 - 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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
BRAINDUMP

SQL: Functioning in the Real World
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 - What You Talkin Bout Willis?
    ( Page 2 of 5 )

    Poor Arnold; in addition to being vertically challenged, he was also beset with frequent ear infections, and never knew what his brother Willis was talking about. Fortunately for him, Mr. Drummond was looted up, and all Arnold really needed to understand was that money talks. And when Arnold wanted to count the oodles of money he would have once old man Drummond kicked the bucket, well...he could have used Aggregate functions to help with that.

    An aggregate function helps summarize large volumes (or even small ones, really) of data. If you have a collection of phone numbers you've collected and want to know how many you have (let's face it, you're a programmer, so you don't) or you want to get a count on the number of days you've spent alone and eating a giant bag of Doritos, you could do just that.

    (Just as a side note: the word "Doritos" is actually in the built-in spell check of Microsoft Word).

    Below is a list of some aggregate functions and what they can do:


    Function Name

    What it Does

    AVG(column)

    Gives you the average value of the column

    COUNT(column)

    Counts the number of rows with data in a column

    COUNT(*)

    Counts the number of selected rows

    FIRST(column)

    Gives the value of the first record in a specified field

    LAST(column)

    Gives the value of the last record in a specified field

    MAX(column)

    Gives the maximum value in a column

    MIN(column)

    Gives the minimum value in a column

    SUM(column)

    Sums up the value of a column

    Average

    I don't have to tell you what average means; you've been that way your entire life. I mean come on, you're no James Marsden. And if you are, please don't sue me for using your name.

    The average function in SQL returns the average value of a given column. Let's say we have a table that lists a person's name, age, and salary and we want to see the average age of the employees listed in the database. Want to follow along? Okay, go ahead and create a table named Employees with the data below:


    Name

    Age

    Salary

    Ang Ree

    92

    $12,000

    Godzilla Nokamura

    45

    $50,000

    Mothra Fujiyama

    27

    $80,000

    King Kong Wong

    19

    $100,000

    Clearly this company has an age discrimination suit waiting to happen. But never mind that for now. I think our table lists a realistic situation, don't you? Coincidentally, all names are fictitious, and any resemblance to real names is purely coincidental.

    Here's the code for the average age:

      Select AVG(Age) from Employees

    This would result in the answer: 45.75.

    Now let's say you are gearing up for that age discrimination lawsuit and need some more data to back you up. Let's say you want to see the average salary of anyone over 30 years old:

      Select AVG(Salary) From Employees WHERE Age>30

    Your answer would be $31,000. You might also wish to see the average salary of those under thirty. To do that, simply reverse the criteria:

      Select AVG(Salary) From Employees WHERE Age<30

    Which results in: $90,000. A clear case for age discrimination.



     
     
    >>> More BrainDump Articles          >>> More By James Payne
     

       

    BRAINDUMP ARTICLES

    - Demystifying SELinux on Kernel 2.6
    - Yahoo and Microsoft Create Ad Partnership
    - The Advantages of Obscure Open Source Browse...
    - Dell Announces CSI-style Digital Forensics S...
    - Milepost GCC Speeds Open-Source Development
    - Learn These 10 Programming Languages
    - Tomcat Capacity Planning
    - Internal and External Performance Tuning wit...
    - Tomcat Benchmark Procedure
    - Benchmarking Tomcat Performance
    - Tomcat Performance Tuning
    - Wubi: Windows-based Ubuntu Installer
    - Configuring and Optimizing Your I/O Scheduler
    - Linux I/O Schedulers
    - Advising the Linux Kernel on File I/O





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek