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 trialWellington Souza
3,121 PointsYour Query Didn't Perform The Correct Count
"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."
This is what I entered and I do not understand what is wrong.
SELECT genre, COUNT(genre) AS genre_count FROM books GROUP BY genre;
3 Answers
Steven Parker
231,271 PointsDon't restrict your count.
You're already grouping by genre, so just count the rows ("COUNT(*)
" or "COUNT(1)
").
Dustin Beaulieu
1,279 PointsI am running into the same issue as above, and my code is identical. I believe the issue is that there is a genre that is null, with a count of 0. I then tried the statement below:
SELECT genre, COUNT(genre) AS "genre_count" FROM books WHERE genre IS NOT NULL GROUP BY genre;
This provided the results I wanted!!! .......but it still says it is not correct. UGH!
Steven Parker
231,271 PointsI believe the challenge wants to see the null genre included, so you need an unrestricted count and no filter ("WHERE
" clause).
Dustin Beaulieu
1,279 PointsThank you for the follow up Steven!
Dustin Beaulieu
1,279 PointsDustin Beaulieu
1,279 PointsThis worked but I do not understand why the result is different. Can you elaborate on this?
Steven Parker
231,271 PointsSteven Parker
231,271 PointsThe
COUNT
function only counts rows that have aNOT NULL
value in the field(s) you specify as an argument, so the original query didn't return the right count for the null genre.