Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Josh Stigall
Josh Stigall
1,493 Points

Reporting the SQL - Count Function - Challenge Question

I'm stuck on the first challenge task in the second challenge section of the Count Function lesson. The question is:

Challenge Task 1 of 2

In the library database there's a books table. There are id, title, author, genre and first_published columns. Count all the books in each genre. Include the genre column first and the genre_count as the second column of information.

I've tried a number of options, the most complex of the group below. (The others are variations of the most complex):

SELECT genre, COUNT(genre) AS genre_count FROM books WHERE genre IS NOT NULL GROUP BY genre ORDER by genre_count DESC;

The main issues I notice:

(1) The query is returning the correct values. I've manually counted them. I'm not sure why this query is not accepted. (2) There is a NULL value that I have both included and excluded from the search. (3) I have ordered ASC and DESC (both genre and genre_count).

I'm afraid I'm missing something very basic; I may even be reading the question incorrectly. At any rate, thanks for any insight you may have.

I have not taken this course, but from a sql standpoint I wouldn't use count(genre) just count() so your sql statement would look as such:

SELECT genre, COUNT() AS genre_count FROM books WHERE genre IS NOT NULL GROUP BY genre ORDER by genre_count DESC;

Hopefully this helps. This is the way I would construct the statement on my sql server

3 Answers

Alexandra Iliuta
Alexandra Iliuta
Courses Plus Student 3,519 Points


you should also count the columns with no genre, so you don't need the NOT NULL condition.

The code:

SELECT genre, COUNT(*) AS genre_count FROM books GROUP BY genre;

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

I'm afraid this is guesswork on my part but try a shortened query like this.

SELECT count, COUNT(genre) AS genre_count FROM books GROUP BY genre_count

Try simply grouping the column ide's since you;re not querying data as such, merely manipulating how the data is reported. :-)

Josh Stigall
Josh Stigall
1,493 Points

Thanks for all of your help!

I tried each of your suggestions and Alexandra's was the one that was accepted as correct!

David's suggestion led to the proper result, but received the same sort of error that I received.

The frustrating part with the challenges is that it seems a specific sort of answer is required, rather than a correct answer. To put it another way, while there are several roads to the proper solution, the system is looking for a specific path, it seems.

It would be helpful if there was more guided feedback in these challenges. Of course, there is not always guided feedback in the real world. I do appreciate the need to think carefully through the formula. It's just frustrating when a formula works, but isn't accepted.