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

SQL Query Issue

Having trouble with query to count all unique genres using count and distinct. Query used does not give desired result.

I am not sure what I am missing with the attached code to get the correct result.

SELECT count(*) AS total_genres FROM ( SELECT DISTINCT(genre) FROM books)

1 Answer

Callan, you didn't give a link, so we're left guessing. But if it is this challenge:

https://teamtreehouse.com/library/reporting-with-sql/aggregate-and-numeric-functions/counting-groups

Then you need two different SQL statements, one for Task 1 and one for Task 2:

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

SELECT COUNT(DISTINCT genre) AS total_genres FROM books;

Sorry about the lack of link. Cheers, I see you include the distinct(genre) within the count rather than in the from.