Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKristian Woods
23,414 PointsI don't fully understand how GROUB BY works...
Hey, from what I understand, the GROUP BY clause groups sets of information together. In the case of this example; he uses the GROUP BY clause along with the COUNT(*) function. Which results in showing all the products and how many products are in that category.
However, I'm struggling to wrap my head around the syntax and what is actually going on...could somebody please explain?
SELECT category, COUNT(*) AS "product count" FROM products GROUP BY category;
thanks
4 Answers
William Parker
24,122 PointsThink of a fruit basket filled with mango, pineapples, and bananas. Now you want to count how many mango, pineapples, and bananas you have, because you feel like having a smoothie party. You would do this by separating or GROUPing each piece of fruit to count how many you have. In this case you have 9 bananas, 2 pineapples, and 3 mango. Time for delicious smoothies!
William Parker
24,122 PointsHey Derek Lin -
Both GROUP BY and DISTINCT returns the same data in there simplest form.
DISTINCT is used solely for giving you unique data. GROUP BY will do the same behind the scenes.
However, if you want to use an Aggregate function (e.g. SUM, MIN, MAX, COUNT, AVG) on the returned data then you must use GROUP BY.
For example, using the table below can you give me the total Salary for London and New York separately using the DISTINCT keyword?
London total Salary is 400 and New York is 700.
+--------------+----------+
| Location | Salary |
+--------------+----------+
| London | 300 |
| New York | 200 |
| London | 100 |
| New York | 500 |
+--------------+----------+
You can only accomplish this using the GROUP BY clause and SUM function, because it will GROUP the 2 related locations together and SUM the salary column.
SELECT Location, SUM(Salary) AS Salary FROM myTable GROUP BY Location;
+--------------+----------+
| Location | Salary |
+--------------+----------+
| London | 400 |
| New York | 700 |
+--------------+----------+
Please take a look at these awesome links below and really focus on the pictorial presentation to visualize what's going on behind the scenes .
Derek Lin
1,563 PointsDISTINCT: gives you unique values in a column/attribute of a row, but unlike GROUP BY does not let you obtain AGGREGATE INFORMATION about unique values. to get information about distinct attributes or entire objects use GROUP BY. yes?
Robert Esteves
1,301 PointsKristian,
The group by function is always used with aggregate functions such as SUM, AVG, COUNT. A group by query resembles a pivot table in MS Excel that it contains dimension (your slicer(s)) and your metrics (your counts). For example; I want to count the number of products that I have per category (product category).
SELECT category, COUNT(*) AS "product count" FROM products GROUP BY category;
So in this case category is your dimension and product count is your metric. Please note that your basic GROUP BY query will summarize your results, but you will lose visibility of the details.
Hope this helps.
Robert
theteamtreehouse
3,472 PointsSomeone should reshoot the video, it's using circular definitions and no one is getting it.
It's like what we had in Engineering Law class, "What would be considered reasonable?", "Well, a reasonable thing is what a reasonable person would do.", "Okay, but what's the definition of a reasonable person?", "Well, a reasonable person would only do reasonable things.", "Yeah, right..."
Kristian Woods
23,414 PointsKristian Woods
23,414 PointsI like the creative approach you took to explain this. Thanks, William!
Derek Lin
1,563 PointsDerek Lin
1,563 Pointsstill confused about the difference between the groupby and distinct.
Xavier Swehla
2,852 PointsXavier Swehla
2,852 Pointslel thx your explanations are the best XD