MySQL
  Home arrow MySQL arrow Page 5 - Speaking SQL (part 2)
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? 
MYSQL

Speaking SQL (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2001-01-18

    Table of Contents:
  • Speaking SQL (part 2)
  • Christmas Presents
  • Teacher's Pet
  • Reading Backwards
  • Count() Me In
  • Like, You Know, Man...
  • Joining Them Together
  • Nest Egg

  • 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


    Speaking SQL (part 2) - Count() Me In


    (Page 5 of 8 )

    SQL also offers a bunch of built-in functions that come in handy when trying to obtain numeric totals and averages of specific fields. The first of these is the very useful COUNT() function, which counts the number of records in the result set and displays this total.

    Consider the following example, which displays the total number of records in the "videos" table:

    mysql> SELECT COUNT(*) FROM videos; +----------+ | COUNT(*) | +----------+ | 6 | +----------+ 1 row in set (0.00 sec)
    This comes in very handy when you need to quickly calculate the total number of records in a table.

    The SUM() function calculates the sum of the values in the result set, while the AVG() function calculates the average. For example, if you wanted to calculate the average grade in math, physics and literature, you could use a query like this:

    mysql> SELECT AVG(math), AVG(physics), AVG(literature) FROM grades; +-----------+--------------+-----------------+ | AVG(math) | AVG(physics) | AVG(literature) | +-----------+--------------+-----------------+ | 74.5000 | 40.7500 | 69.0000 | +-----------+--------------+-----------------+ 1 row in set (0.00 sec)
    You can identify the smallest and largest value in a specific column with the MIN() and MAX() functions - the following queries display the lowest and highest grade in math respectively.

    mysql> SELECT MIN(math) FROM grades; +-----------+ | MIN(math) | +-----------+ | 65 | +-----------+ 1 row in set (0.00 sec)

    mysql> SELECT MAX(math) FROM grades; +-----------+ | MAX(math) | +-----------+ | 96 | +-----------+ 1 row in set (0.00 sec)


    This article copyright Melonfire 2001. All rights reserved.

    More MySQL Articles
    More By icarus, (c) Melonfire


     

       

    MYSQL ARTICLES

    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...





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