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

Databases

Alan Ayoub
Alan Ayoub
40,294 Points

Reporting 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
Andrew Winkler
37,739 Points

Hi 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;
Alan Ayoub
Alan Ayoub
40,294 Points

Nice! Thanks Andrew!

Andrew 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;

You don't need to add the GROUP BY function

This passed:

SELECT COUNT (DISTINCT genre) AS total_genres FROM books;

something go wrong with my code SELECT COUNT(DISTINCT genre) AS genre_count FROM books;

SELECT COUNT (DISTINCT genre) AS total_genres FROM books;