Untitled-4

iphone apps

Essential iPhone Apps

We run down the best apps in each category. Get the most out of your iPhone today!

Read More

New-Chrome-Icon

Google Chrome - The Official Browser of Gounce.com

Do yourself a favor and check out this fantastic browser from our friends at Google.

Read More

smartdjvsgenius

Itunes' Genius Vs. Zune's Smart DJ

Too lazy to make playlists? Try these automated tools then kick back and enjoy!

Read More

running

Best Songs To Run To 2011

These songs, sorted by genre, will keep you headed in the right direction.

Read More

prezivsppt

Prezi vs. Microsoft PowerPoint

Deliver astounding presentations from anywhere, by harnessing Prezi's state of the art cloud technology.

Read More

Friday, 24 June 2011 00:48

MySQL: Using the Group By Function

This week, I will be discussing how the group by function actually works in MySQL. For whatever reason, when I first started learning MySQL, I had difficulties grasping grouping concepts. Looking back at those times now, I feel hours of study time could have been avoided had someone taught me the proper way of approaching the group by clause as well as other aggregate functions. I hope this tutorial solves these issues for you, and you are able to both quickly learn and retain how grouping works in MySQL.

Grouping functions include: AVG, COUNT, MAX, MIN, STDDEV, SUM, VARIANCE. I assume you have some general knowledge on how these work (their pretty straight forward), but let us look at a quick example:

SELECT AVG(salary), MAX(salary), MIN(salary), SUM(salary)

FROM   employees;


In the above example, we are focusing on some arbitrary table called employees. We are using the aggregate functions AVG, MAX, MIN, and SUM on the salary column of the employees table. A table will be returned with 4 columns, named AVG(salary), MAX(salary), MIN(salary), SUM(salary) respectively. Each of these columns will contain a single value under them (the next row so to speak), that will contain the average of the salary row, the maximum value in the salary row, the minimum value in the salary row, and finally the sum of the salary row. It’s not a very difficult concept, but when the GROUP BY clause gets introduced, some get confused. Let’s have a look at the next example which includes the GROUP BY clause. 

SELECT  department_id, AVG(salary)

FROM employees

GROUP BY department_id ;

In this example, we are retrieving two columns from the arbitrary employees table. One of these columns will be detpatment_id, while the next will be the average salary. Now usually, average salary would yield one value (the average of the salary column, a single value). But the group by function changes this. Instead, the AVG function takes the average of each individual department id. So for example, if there were two departments, 1 and 2, the AVG function would take the average of the salary of all the salaries that are from department one, display this value, and then do the same for department number 2. The group by clause acts on only the aggregate functions in the select statements. It performs the aggregate function on the rows which have the same department ID only. 

That should sum up how to use the GROUP BY clause. Of course things can get far more complicated, but this is my basic approach of how I look at using the GROUP BY cause. I hope this tutorial was useful.


Like our style? Subscribe and we'll keep you in the loop!

* indicates required

Last modified on Sunday, 26 February 2012 00:06
Rev

Rev

Interests: Computers, Programming, Hockey, Football, Music

Hobbies: Guitar, Drums, Programming, Hockey

Favorite Movie: Memento

Favorite Animated Movie: Toy Story

Favorite Album: Fear of a Blank Planet - Porcupine Tree

Gear: Macbook 6.1

Instruments: PRS Angelus Acoustic, PRS Custom 24 Electric, Pearl Export Drums.

Website: www.gounce.com Contact Rev
blog comments powered by Disqus

Follow Us

GoogleBuzz     delicious youtube

Recent Articles

Step By Step Links