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.





It's hard not to laugh when you see this. It is kind of ironic, wouldn't you say? I mean after such a great season with…
St. Patrick's Day is a Christian holiday celebrating the life of Saint Patrick; he is associated with bringing Christianity to Ireland in the 5th century.…
Make St. Patrick's Day explosive with a few Irish Car Bombs!
Food Network Canada has failed at its attempt to replicate the American Food Network's "Diners, Drive-Ins and Dives". While there's no denying that shows revolving…
Alright, so we’ve got the essential basics down. We’ve created out first frame and it has some text in it using JLables. Now it’s time…
In this technical age we often find ourselves looking forward to the next best thing, but to improve your shave you will have to look…
The summer is in full swing and I am often asked to convert between Celsius and Fahrenheit. The exact calculation is: [(Degrees Celsius)*(9/5)] + 32…
Okay, so here's the deal. Your collection of PCs, and laptops started many years ago. You have had several "favorite" computers. Fast forward to present…
Hyperwords is a slick extension that makes any word on a web page into a hyperlink! Pretty neat idea, but does it hold any water?…
As an individual who spends countless hours online, I am a little ashamed to admit that I struggle to differentiate between the forward slash and…
Make St. Patrick's Day explosive with a few Irish Car Bombs!