FAQ: Code Challenge: Aggregate Functions - Code Challenge 9

This community-built FAQ covers the “Code Challenge 9” 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 9

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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!

SELECT MAX(watch_duration_in_minutes) AS ‘MAX Duration’, MIN (watch_duration_in_minutes) AS ‘MIN Duration’
FROM watch_history;

The above code is correct but just curious why this is wrong:

SELECT watch_duration_in_minutes AS ‘Duration’, MAX(Duration), MIN(Duration)
FROM watch_history;

What if I wanted to know the user_id and watch_date of both the longest and shortest event? Can this be done in one query?

My instructions say to rename the results. When trying to add the AS statement it would not accept my answer. When I clicked the hint it did not have the AS statements. So why is it telling me to rename anything? Did anyone else have this issue or did I just massively over think this test?

I thought the same thing. I had renamed them and it was failing me. Then I noticed that the order of the MIN, MAX makes a difference. If you use MIN first it fails but MAX first succeeds, named or not.

Use AS. Once a column has been renamed with AS, the new name will work in the later steps in your query.

SELECT long_name AS short_name 
FROM my_table
WHERE short_name > 30;

Can someone explain the correct code for renaming the new columns by adding AS in the last exercise?

What if I want to see each user whom has recorded max(watch_duration_in_minutes) and whom has recorded minimum of it?
Is it possible in SQL??

Hi everyone! Who could help and explain this problem?

Code is: SELECT MIN(watch_duration_in_minutes) AS ‘min’, MAX(watch_duration_in_minutes) AS ‘max’
FROM watch_history;
I’ve got the result

But I have this error: Query should return the maximum and then the minimum watch durations, in one query.

Ok, problem is solved. Just swapped function MAX() and MIN().
Correct code is:
SELECT MAX(watch_duration_in_minutes) AS ‘max’, MIN(watch_duration_in_minutes) AS ‘min’
FROM watch_history;

After run the collect query, the result appear, max and min.
Are those results different ‘user_id’(or ‘id’)?

When I tried to put ‘SELECT user_id …,’ in the first line, the result was just ‘user_id’ is added on the left. It looks like same user_id happen to have max and min at the same time?

I guess that each column shows independent results.

mistype, in the first line, not collect query, but correct query.

Hello to every/anyone, I have a questions regarding column referencing, specifically from question 6 of this exercise

I had tried to use the following syntax for my answer, which would be correct if i simply didn’t use the column references, but is showing up as wrong when I replace the columns in the select statement with numbers. I tried tinkering with it and it boiled down to the SUM function in the SELECT statement - apparently I can reference the user_id column and I’ll get a resulting query, but once i reference the SUM column, the query fails (even without the alias). Anyone know why?

SELECT user_id, SUM(watch_duration_in_minutes) AS ‘total_minutes’
FROM watch_history
HAVING 2 > 400;

Any help would be appreciated!

EDIT: looks like you can’t reference a column as a number if you’re using operators because SQL won’t see “2” as a reference. Can someone confirm this?

SOLVED: confirmed in next section (How to Hack Hacker News) - HAVING doesn’t support aliases so you’d need to spell out the entire aggregate function column.