Help with understanding how (*) works

Hello,
I’m learning SQL, so far everything is fairly easy to understand but I have one thing thats troubling and I didn’t found any answer neither here, ad on google.

Im doing project called: How to Hack Hacker News
There is a code line:
SELECT strftime(’%H’, timestamp),
AVG(score),
COUNT(*)
FROM hacker_news
GROUP BY 1
ORDER BY 1;

MY question is, how SQL knows what it should count with ( * )? Why it doesn’t count the ‘avg score’, or ‘hours’ for example? If I don’t want to use ( * ) what should i write inside to make it work as well ( ) ?

It’s not only in this example, usage of ( * ) puzzled me few times so far. I understand what this command does, I just don’t understand how ( * ) is choosing what column to consider at given query.

Thanks for help.

Hello @barelybaked, welcome to the forums!
The * operator is a wild-card, meaning it can mean anything. In the case of COUNT, it means, “count of all of the rows in the table.” Say a certain column only had a few values in it (like a table-with app user data in it-where the last column is “has premium”). In this situation, you might want to know how many people have premium, so you’d do something like this:

SELECT COUNT(HasPremium)
FROM AppUsers;

I hope this helps!

2 Likes

Thanks for your answer mate. I still at times don’t know what * will do at given moment, but maybe the feel for wild card will come with more practice.

1 Like