A question on a SQL project called "Trends in startups"

One request for this project is:
Next filter the result to only include categories that have more than three companies in them.
What are the most competitive markets?

My code is the following but the terminal returns nothing:

SELECT category, COUNT(*) 
FROM startups
GROUP BY category
HAVING 2 > 3;

The hint gives and it works:

SELECT category, COUNT(*)
FROM startups
GROUP BY category
HAVING COUNT(*) > 3;

My question is: why cannot HAVING 2 (referring to the second selected column COUNT(*) in this case) work? Thanks!

The problem is that the code has no way to parse in which context you mean the numbers in HAVING! Either you mean to group by category where the number 2 is bigger than the number 3, which is obviously impossible. Or you mean to group by category where column 2 is bigger than column 3, which is also impossible as column 3 does not exist! In general using numeric column references in HAVING and WHERE statements can be tricky, as they only work when you aren’t comparing to other numerics.

1 Like