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.
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!)
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.
In the code editor, type:
SELECT price, COUNT(*)
FROM fake_apps
GROUP BY price;
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.
Remove the previous query. Write a new query that calculates the total number of downloads for each category.
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.
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
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.
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.