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 trialAlan Ayoub
40,294 PointsReporting with SQL....
I'm totally stuck here, I feel like the DISTINCT explanation was like 3 seconds? I have no idea what I'm doing wrong!
Challenge Task 2 of 2
In the library database there's a books table. There are id, title, author, genre and first_published columns. Write a query to count all the unique genres in the books table. Alias it as total_genres. Type in your command below.
SELECT DISTINCT genre, COUNT(*) AS genre_count FROM books GROUP BY genre;
4 Answers
Andrew Winkler
37,739 PointsHi there. I agree it was brief, but here's some help. Click here for documentation.
The COUNT(DISTINCT column_name) function returns the number of distinct values of the specified column:
SELECT COUNT (DISTINCT column_name)
FROM table_name;
Spoiler Alert! -- The answer is below.
...
...
...
...
SELECT COUNT (DISTINCT genre)
AS genre_count
FROM books
GROUP BY genre;
Erik Sorensen
13,846 PointsThis passed:
SELECT COUNT (DISTINCT genre) AS total_genres FROM books;
Aya khaled
Courses Plus Student 1,967 Pointssomething go wrong with my code SELECT COUNT(DISTINCT genre) AS genre_count FROM books;
Ashton Shingirirai Calton Munkondya
3,925 PointsSELECT COUNT (DISTINCT genre) AS total_genres FROM books;
Alan Ayoub
40,294 PointsAlan Ayoub
40,294 PointsNice! Thanks Andrew!
Charlie Harcourt
8,046 PointsCharlie Harcourt
8,046 PointsAndrew I have tried your code and something seems to be going wrong. There doesn't need to be a GROUP BY and the task is asking for total_genres not genre_count. :)
SELECT COUNT (DISTINCT genre) AS total_genres FROM books;
Taylor Henson
855 PointsTaylor Henson
855 PointsYou don't need to add the GROUP BY function