FAQ: Code Challenge: Aggregate Functions - Code Challenge 7

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

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!

for the lats several exercises I have written the code, but am given an error. I don’t know what I did wrong, Can’t figure it out, I look at the hint and even after making it look just like the code it doesn’t work, It isn’t until copy a pasting the code from the hint that it compiles,

Can someone tell me what I did wrong here?

Very curious! I don’t see where there is an error… What is interesting is the SQL error in bottom right corner… I just ran exact code and had no issue.

Hello,

Why can’t we use HAVING status = ‘paid’ under the GROUP BY statement instead of using the WHERE statement?

10 Likes

I have the similar situation with this ex. and with the previous one too. Have you find a solution?

I had the same problem, my code was the same as what was suggested in the hint, and it did not work.

I am current;y having the same issue using my code below
SELECT SUM(amount), pay_date
FROM payments
WHERE status = ‘paid’
GROUP BY 2
ORDER BY SUM(amount) DESC;

What is going on?

You just need to have no spaces in the column name: ‘total_amount’

1 Like

They want the pay_date column first, amount second.

1 Like

I was wondering if you could write something like:

select pay_date as ‘date’, max(sum(amount)) as ‘amount’ from payments
group by 1;

To select only the date and amount where the most was paid.

It doesn’t seem to work. I’m not conversant enough with SQL to see why, but you could get the same information with

SELECT pay_date AS date, sum(amount) as amount  
FROM payments
WHERE status = 'paid'
GROUP BY date
order by amount DESC
limit 1;
1 Like

Why can’t we use HAVING , which seems to give the same answer ?

select pay_date, sum(amount) as total_amount
from payments
group by pay_date
having status=‘paid’
order by total_amount desc;

Just started SQL, made the same code with ‘HAVING’ but don’t know what’s wrong :frowning_face:

I thought the same thing myself, but notice that the results of the correct answer are different than the results using HAVING status = 'paid'.

Since WHERE filters rows, you want to use WHERE here because you only want to include rows that have status paid when taking the sum() of the payment values.

If you use HAVING, you’re filtering the GROUP BY results, so I suppose you would be including rows whose status is not paid in the sum calculation, thus giving you different (and consequently incorrect) results.

1 Like

was wondering why can’t I use having to solve this problem instead of where.
I tried with Having and got the same result however, in the workspace it returns an error.
Any suggestions on this will be really helpful.

the result is actually different, if you look at the total amounts column

i have the same question , as instructed in the lessons HAVING is used for groups so here we should be using HAVING

Capture 07

Why does the WHERE clause need to be
WHERE status = ‘paid’
instead of WHERE status LIKE ‘paid’?
I figured either should work but the WHERE + LIKE combination was incorrect.