Skip to content

SQL 101 – Summarizing Data

2005 October 20
tags: , ,
by Shannon Lowder

This is the last of the lessons on functions for the 100 level.  I hope you’ve enjoyed them so far, but it’s time to wrap these up!

When beginning to learn SQL, it won’t be long until you have to answer questions that require you to summarize the data.  It’s one of the primary reasons SQL is used.  You store many records detailing events, then you can summarize that data and report it back to users, so they don’t have to summarize it by hand.

I’m going to show you the 5 most common ways you’ll be asked to summarize data: COUNT, SUM, MAX, MIN,and AVG (Average).  There are several more that SQL understands, and once you learn to define your own functions, there will be an
infinite number of ways to summarize that data.  I just want you to gain an understanding of the basics, so you can go on to all those other methods.

COUNT

There are three variations to this function

COUNT(*)
COUNT(ALL expression)
COUNT(DISTINCT expression)

The first method is the most common. You’ll use it when you want to count how many rows are in a table.  If we ran the following query against our products table we’ve referred to throughout the SQL 101 lessons, we get 4, since we only defined four records.

SELECT
   COUNT(*) AS [count]
FROM products
count
-----
4

We would get the same result from the following query.

SELECT
COUNT(ALL productName) AS [count]
FROM products

count
-----
4

If there were duplicates in the products table, and we only wanted a count of unique values, then the following version would let us count only the unique values in the table.

SELECT
COUNT(DISTINCT productName) AS [count]
FROM products

count
-----
4

Since there are no duplicates in our example table, we still get four.

SUM

Like COUNT, the sum function accepts ALL or DISTINCT as a modifier, but by default, it will sum all.  Sum is the first function that really interprets the data.  A COUNT is simple, a SUM actually does a little work for you.  Warning,
make sure the column your summing is a numeric type.  The sum function will try to convert the column into a numeric type, and if it finds even one value that cannot be converted, you will get an error.  You may also want to consider using the ISNULL function with this, since adding NULL to something is still NULL.

SELECT
SUM(price) AS [Total Price]
FROM products

Total Price
-----------
3.49

MAX, MIN, and AVG (average)

SQL is can be a great way to store lots of detail records. Whenever you have data, someone will eventually ask you to do some statistical analysis on that data. MAX, MIN, and average are the most common statistical numbers asked for.

SELECT
MAX(price) AS [max]
, MIN(price) AS [min]
, AVG(price) AS [average]
FROM products

max     min     average
----    ---     -------
1.25    .25     .8725

Conclusion

Summarizing data will be a big part of your life as a SQL developer.  These functions will serve as your first steps into learning more and more powerful ways to summarize data.

If you have any questions, please, feel free to send them in!  I’m here to help you learn as much as you can about SQL.

Previous: Calculated Fields Next: GROUP BY
7 Responses leave one →
  1. vallibe permalink
    December 19, 2005

    hi| aggregate functions r the useful one. they perform calculation on a set of values and return a single value.

  2. RPhillips permalink
    December 19, 2005

    Good Work. what’s next?

  3. jtutoff permalink
    December 19, 2005

    Thank you

  4. luthfi permalink
    May 16, 2012

    can AVG function find calculate money data type? since i tried it always appear error like this:

    select MIN(networth), MAX(networth), AVG(networth) from MOVIEEXEC;
    ERROR: function avg(money) does not exist
    LINE 1: select MIN(networth), MAX(networth), AVG(networth) from MOVI…
    ^
    HINT: No function matches the given name and argument types. You might need to add explicit type casts.

    any suggestion for this issue? thank you in adavance :)

    • May 16, 2012

      What version of SQL are you running? I ran the following test in SQL 2008R2 and was about to get results without an error. Can you send me the definition of your networth column?

      USE tempdb
      GO
      
      CREATE TABLE MOVIEEXEC (
        id INT IDENTITY(1,1)
        , Name varchar(255) 
        , networth money 
       ) 
      INSERT INTO MOVIEEXEC (Name,NetWorth) VALUES ('Rosa, Dora Q.','3786414');
      INSERT INTO MOVIEEXEC (Name,NetWorth) VALUES ('Moon, Keefe B.','358193');
      INSERT INTO MOVIEEXEC (Name,NetWorth) VALUES ('Hess, Autumn E.','2678358');
      
      SELECT 
      	MIN(networth) AS MinNetWorth
      	, MAX(networth) AS MaxNetWorth
      	, AVG(networth) AS AVGNetWorth
      FROM MOVIEEXEC;
      

Trackbacks and Pingbacks

  1. SQL 101 | Shannon Lowder
  2. SQL 101 – GROUP BY | Shannon Lowder

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS