How to Use sql Aggregate Functions

Aggregate means a whole, the term is most used in economics (if you have ever taken an economics class, you probably remember how painful it was :)), it basically means ‘as a whole’. So, we’ll cover a few simple aggregate functions , avg,max, and min.

Remember the price we concatenated with a string? What if we wanted to see the average price of all our the products in our products table?

SELECT AVG(price) AS average_price FROM products;

the query would return something along the lines of 23.94443, obviously depending the prices in your table , if it were cars, it might be 23,232.2238576.

Notice we also used an alias in there.

Now , what if we wanted to find the minimum and maximum price in our products table?

SELECT MIN(price) AS mini_price, MAX(price) AS max_price FROM products;

The output should be two numbers let’s say : $3.99 $599.99

That’s it, aggregate functions are often overlooked as an easy way to get quick info.

Bookmark it and Share These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • BlinkList
  • Bumpzee
  • De.lirio.us
  • Furl
  • Ma.gnolia
  • NewsVine
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati
  • YahooMyWeb
Filter Data Using the Having ClauseSQL’s Between Range

2 Comments so far

  1. SQL’s Between Range | SQL Tutorials on February 16th, 2008

    […] have previously found the minimum and maximum range for a price column in a […]

  2. […] how used the AVG aggragate function to get the average […]

Leave a reply