FAQ: Code Challenge: Aggregate Functions - Code Challenge 5


#1

This community-built FAQ covers the “Code Challenge 5” exercise from the lesson “Code Challenge: Aggregate Functions”.

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

Web Development
Data Science

FAQs on the exercise Code Challenge 5

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!


#2

Confused why my query isn’t working here:

I think it is because I am trying to call on the second column as 2. Other than that, my query is the same as the hint. Are there circumstances where you can’t call on the column number like this?


#3

I got the exact same problem, I’m guessing that when you want to use actual values of number (in this case 400)other than notation related by a number(like 2 in this incident) in the same sentence, it doesn’t work. Since ‘Group by 1’ works just fine.

But I might be wrong.


#4

Just wondering why using the clause AS is necessary to get a Query result?
i.e This will get me nothing:
SELECT user_id,
SUM(watch_duration_in_minutes)
FROM watch_history
GROUP BY 1
HAVING(watch_duration_in_minutes) > 400 ;

Whereas
SELECT user_id,
SUM(watch_duration_in_minutes) AS ‘total_duration’
FROM watch_history
GROUP BY 1
HAVING(total_duration) > 400;

Works perfectly. From what I can see AS renames a column but why would that change me receiving the data? Is it because without a column for it to place data it gets confused?


#5

you forgot in your first solution after HAVING the sum aggregate. in the second solution it is included with the alias name


#6

same here. it must be work, but how? some pro here?


#7

Noob here, but I believe we can’t use the alias “2” in place of SUM(watch_duration_in_minutes) because the function…

HAVING 2 > 400

…attempts to process those numbers as values rather than an alias placeholder, which produces an error.

I read just now that basic SQL allows alias references only in ORDER BY commands, and that the HAVING clause is processed before the SELECT clause. Therefore, attempting to use an alias reference number in a HAVING clause produces an error because the column the user wants to reference has not yet been processed and assigned that numerical value . . . and even then, I think the system might not have a clear-cut way of differentiating whether you’re intending the number 2 to be an alias reference or a number interacting with the column you’re numerically assessing.

I don’t know the logic as to why, but perhaps this Codecademy course will make it clear in time. Thus far the course has not mentioned that the lines of code are not necessarily processed left-to-right, top-to-bottom, despite the linear learning method suggesting otherwise.