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 trial

Development Tools Database Foundations SQL Calculating, Aggregating and Other Functions Grouping, Joining and Cleaning Up

Daniel Sokol
Daniel Sokol
14,888 Points

Not quite getting "Group By".........

In the video, Andrew says: "when you select a regular column, it will just return the first value in that column."

What does he mean by a "regular column"? Is there an irregular column? Aren't they all regular?

And why does it only return the first value? Why not return all the values if you are selecting the entire column?

How does 'GROUP BY' solve this problem?

Thanks!

2 Answers

Steven Parker
Steven Parker
229,982 Points

What he means is "when you select a regular column along with an aggregate function"

He's saying "regular column" to distinguish it from an aggregate function column. The previous example selected only aggregate functions.

It would return all columns without the aggregate function, but the aggregate function only makes sense when it is applied to multiple rows.

Using "GROUP BY" causes the aggregate to be applied to the rows in groups instead of all at once, which yields a separate output row for each group.

Make sense now?

Daniel Sokol
Daniel Sokol
14,888 Points

Thank you, Steven, for your answer. To clarify, are you saying that if you select a column WITHOUT a function applied to it like simply: 'title', and another column WITH a function like MAX(score), you must use GROUP BY to see all the results of the 'title' column?

To clarify further: GROUP BY is used when you have one query where you are selecting a 'regular column' (no function), along with a column that has a function applied to it ?

And if the above is correct, would GROUP BY always be applied to the "regular column"?

Thanks!