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 trialDonnie Driskell
2,243 PointsCan't figure out COUNT() for challenge task #1
https://teamtreehouse.com/library/reporting-with-sql/aggregate-and-numeric-functions/counting-groups
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 my query: SELECT genre, COUNT(genre) AS genre_count FROM books GROUP BY genre;
This is my Result: Bummer: didn't return the correct results However, the table that it returned had two columns genre and genre_count. The only problem I see is that there is a missing genre (?)
If you run my query, you will see the empty space that has "0" in genre_count. So then I tried to run a query that would be WHERE genre_count => 0; and still no go.
thank you for your help. Donnie
3 Answers
Rashmi Jha
2,842 PointsSELECT DISTINCT genre, COUNT(*) AS "genre_count" FROM books GROUP BY genre;
use DISTINCT keyword with genre. This query works for me i hope this will work for you too.
Donnie Driskell
2,243 PointsThank you Rashmi. Yes, that query worked for me.
Donnie Driskell
2,243 PointsThank you Curtis !
Curtis Vanzandt
9,165 PointsCurtis Vanzandt
9,165 PointsFor what it's worth, the reason why this works is because in the following:
SELECT genre, COUNT(genre) AS genre_count FROM books GROUP BY genre;
Counting the genres ignores all the null genres. That's why when you run back, it starts with a count of 0 for the lack of genre.