SunQuest
 
       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  
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 
Actuate Whitepapers 
VeriSign Whitepapers 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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


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