Home arrow MySQL arrow Page 5 - Speaking SQL (part 2)

Count() Me In - MySQL

After learning how to insert and edit data in a database, thesecond part of our SQL tutorial takes an in-depth look at the SELECTquery,and explains how to use joins, sub-queries and built-in functions tofocus in on the data you need.

TABLE OF CONTENTS:
  1. Speaking SQL (part 2)
  2. Christmas Presents
  3. Teacher's Pet
  4. Reading Backwards
  5. Count() Me In
  6. Like, You Know, Man...
  7. Joining Them Together
  8. Nest Egg
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 5
January 18, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

blog comments powered by Disqus
   

MYSQL ARTICLES

- Xeround Releases Free Version of MySQL Cloud...
- Oracle Announces New MySQL Specialization
- Constant Contact Chooses SkySQL for MySQL Su...
- Revoke Statement in MySQL
- The Grant Statement in MySQL
- SuccessBricks Announces ClearDB Availability...
- Building a PHP ORM: Deploying a Blog
- TROSYS Launches Free MySQL Manager and Admin...
- Building an ORM in PHP: Domain Modeling
- Building an ORM in PHP
- MySQL Leads Open Source Market, Gets Cluster...
- Oracle Announces Milestone Release for MySQL
- How to Stop SQL Injection Attacks
- New Defragmentation Solution for SQL Server
- Comparison of MyISAM and InnoDB MySQL Databa...


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap

Dev Shed Tutorial Topics: