FAQ: Aggregate Functions - Group By I

This community-built FAQ covers the “Group By I” exercise from the lesson “Aggregate Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development
Data Science

Learn SQL

FAQs on the exercise Group By I

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Please Help.

If we were to not use the GROUP BY clause such as

SELECT category, SUM (downloads)
FROM fake_apps;

The program returns 1 row with with the News in the category and the sum of the downloads in the other column.

But why does it select News as the category? It is not the min, max, mode, or avg.

3 Likes

Just wanto to bump this question as I had the exact some one in mind coming here. I think understanding this is fundamental to understanding how SQL works/its logic for organizing queries.

Any reply is very well received! <3

Well since SUM(downloads) only returns one value which is the sum of all category’s downloads, it only has a corresponding value for the SUM(downloads) which is the first value in category (here: news!)

1 Like

In the context of this exercise, why should we type an * inside the COUNT( ) function? Does it make any difference?

SELECT price, COUNT(*)
FROM fake_apps
GROUP BY price;

@ justin976629
I believe it selects News because it is the category with the fewest letters.

Hi guys,

I have a question regarding the following exercise GROUP BY I.
I was struggling with the second task and the exercise was only completed when the solution was found for the third task which was a bit confusing. Please advise about the second question as I couldn’t find any solution with WHERE clause which works.

  1. In the code editor, type:

SELECT price, COUNT(*)
FROM fake_apps
GROUP BY price;

  1. In the previous query, add a WHERE clause to count the total number of apps that have been downloaded more than 20,000 times, at each price.

  2. Remove the previous query. Write a new query that calculates the total number of downloads for each category.

Select category and SUM(downloads) .

SELECT category,

SUM(downloads)

FROM fake_apps

GROUP BY category;

1 Like

You need to perform filtering with WHERE before the GROUP BY clause. The hint for that question notes this. It’s probably worth looking up execution order for sql anyway as it makes a few things clearer if you’re unfamiliar with it.

Thanks for your advice.

Hi, there.
Im just wandering is there any difference in this code if:
select sum(downloads) ,category
rather than
select category , sum(downloads)
coz the second one is not working in codecademy
https://gist.github.com/0b8d2a50bff4a63c8b1efc8cef4679f6

1 Like

I came here to ask exactly this.

SELECT SUM(downloads), category
FROM fake_apps
GROUP BY category;

returns the same result as

SELECT category, SUM(downloads)
FROM fake_apps
GROUP BY category;

just with the columns switched around. I guess, maybe, Codecademy is trying to reinforce writing queries that take into mind how easy the results will be to read? Though that seems like a stretch; there are plenty of use cases for wanting to list a column of sums first.

1 Like

I have this same question. It only shows the answer for the third section but not the second. I’m still trying to figure it out.

So when using the GROUP BY clause is it sort of acting like the the DISTINCT clause? Or is it mostly just telling which column/s I’m telling it to GROUP BY.