FAQ: Aggregate Functions - Having

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

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!

Hi,
How come we use * with clause COUNT to count number of apps at each price point. I thought we need to use COUNT(id ) or COUNT(name)? Don’t we use * to include everything? Thank you

2 Likes

I had a similar questions. I am not understanding why HAVING COUNT(*) > 10… Infact I used HAVING COUNT(price) > 10 and the task was checked off as if done correctly. It was not until I looked at the hint that I realized I had done the query ‘wrong’ (even though the task was ‘completed’ when I used HAVING COUNT(price) > 10)

1 Like

Same here. Would still love an answer if anyone has it?
Thanks.

1 Like

The difference between the COUNT (*) function and COUNT (column name | expression) is that the second one (like the other aggregate functions) does not take into account NULL values ​​in the calculation.

3 Likes

I had this same question, after coming here and not finding an answer I played around with the code a bit and it helped me understand more.

Anyone else having this same issue I’d recommend running the code with just

SELECT price, ROUND(AVG(downloads)), COUNT (*)
FROM fake_apps;

and seeing what the “count” function is doing.

So when you run

SELECT price, 
   ROUND(AVG(downloads)),
   COUNT(*)
FROM fake_apps
GROUP BY price
HAVING COUNT(*) > 10;

Basically you’re saying, select these rows and count them, then group them by price, for the ones that have more than 10 rows in their group.

Sorry I can’t put it more succinctly lol but that’s what helped me!

1 Like

Why can’t we use where clause instead of having? in the example given, we can clearly get the condition by usnig where count(*) > 10;

Having is specifically for aggregate functions. Where, WHERE is for data points in just a regular old query

Why can’t I do column number in HAVING? Is it only in GROUP BY and ORDER BY?

SELECT price,
ROUND(AVG(downloads)),
COUNT(*)
FROM fake_apps

Then I do
HAVING 3 > 3?

It’s answered here: What's the difference between COUNT(1), COUNT(*), and COUNT(column_name)?

2 Likes

Would also like to know…

Hi, what is average downloads in this case? We have exact number of downloads for every specific price value and instance. Average of this number is this number. This doesn’t seem to make sense.
Thank you

 Select price, 
 
   Round(Avg(downloads)),
 
   COUNT(*)
 
 From fake_apps
 
 Group by price
 
Having count(price)>10;
1 Like

Is there any restrictions on column references in HAVING?

For example, instead of-

SELECT price,

ROUND(AVG(downloads)),

COUNT(*)

FROM fake_apps

GROUP BY price

HAVING COUNT(*) > 10;

why cant I use-

SELECT price,

ROUND(AVG(downloads)),

COUNT(*)

FROM fake_apps

GROUP BY price

HAVING 3 > 10;

1 Like

Hi
so this is the right code
select price, round (avg(downloads)), count (*)
from fake_apps
group by price
having count(*) > 10;

however, since the question was Add a HAVING clause to restrict the query to price points that have more than 10 apps.

I was wondering, why cant the last statement be
having downloads > 10; but when i ran that the table showed a value = '9’ under the count()* column

can someone explain why?

Hi. I was actually making the same mistake. We’re confusing the number of downloads with the number of apps.
When using HAVING downloads > 10 and we get that row with the ‘9’ value, it means there are 9 apps which have more than 10 downloads each. It might be helpful to take a look at the entire fake_apps table where you’ll see EVERY app has more than 10 downloads.

1 Like

I can’t reference columns like this, can I?

SELECT price, ROUND(AVG(downloads)), COUNT(*)
FROM fake_apps
GROUP BY price HAVING 3 > 10;

Is there other way to avoid duplicating COUNT(*)?

1 Like

I finally used the forums to try and find this exact question.

I think I figured it out:
If you query all of the table, you’ll see that the prices aren’t random and that there’s only a handful of price points. When you take the average, you’re taking the average of all downloads for apps at that price point.

For example the average downloads of all apps that cost .99 cents

Curious about this too! Why can’t you use column references in HAVING clauses?

1 Like

I am having the same question on why we are needing a count function as well. I believe if we used a statement of having name > 10; It provides us with the same answer.

I too have a questions similar to the one here: FAQ: Aggregate Functions - Having - #13 by cloudsolver36218

Is it possible to substitute HAVING 3 > 10 for HAVING COUNT(*) > 10?

Thanks!

1 Like