FAQ: Code Challenge: Aggregate Functions - Code Challenge 2

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

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!

What is the count variable and why is it a 2 in this case?

3 Likes

COUNT(*) AS ‘count’ is the second column in the output table.
We want to sort base on popularity, so we sort based on the count, descending order.
To do this, we use ORDER BY. And now we need to reference the second column, which has all the count.
We can reference it with the number 2 - second column. Try to replace this with 1, and you will see it is sorted alphabetically because 1 refers to the first output column, or the names.
Also try to replace 2 with ‘count’ (without the quotes) - it still works. This is because the column is named ‘count’. Remember the first line, AS ‘count’. This is the name of the column.
Hope this helps.

Edit: It is called a column reference. Read more online.

4 Likes

As long as each ‘first_name’ belongs to a real user and at the same time each user in the table ‘users’ has a unique id, why in the solution of the exercise we don’t simply write :

COUNT (id) FROM users instead of COUNT(*) AS ‘count’ FROM users ?

I suppose it would be simplier and more precise. Moreover I think that actually, this is the usefullness of the ‘id’ parameter , to count the number of users, no matter what the statement is.

1 Like

Hi, when we are told:
What are the most popular first names on Codeflix?

Use COUNT() , GROUP BY , and ORDER BY to create a list of first names and occurrences within the users table.

Order the data so that the most popular names are displayed first.

I did:

SELECT COUNT(first_name), first_name
FROM users 
GROUP BY first_name
ORDER BY first_name DESC;

But the answer is:

SELECT first_name, COUNT(*) AS 'count'
FROM users
GROUP BY first_name
ORDER BY 2 DESC; 

Is there something wrong with what I did?

there seems to be a difference:

https://stackoverflow.com/questions/3003457/count-vs-countcolumn-name-which-is-more-correct

but i don’t think you have NULL values.

2 Likes