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 trialHIDAYATULLAH ARGHANDABI
21,058 PointsIs there a mistake in my code > SELECT DISTINCT genre, count(*) as genre_count FROM books GROUP BY genre;
SELECT DISTINCT genre, count(*) as genre_count FROM books GROUP BY genre;
can some one help
3 Answers
Steven Parker
231,269 PointsNow that you have shown the actual task, I can give you more specific hints:
- the challenge asks only for the count, so you won't need to select "genre"
- you also won't need the ""GROUP BY`" clause
- the challenge asks you "to count all the unique genres", so you'll need to specify the field to count
- you will still use the word "
DISTINCT
", but as part of the count argument
HIDAYATULLAH ARGHANDABI
21,058 PointsSELECT distinct genre, count(*) as total_genres FROM books;
is this what u mean sir
Steven Parker
231,269 PointsAs I hinted, you don't need to select "genre" as a separate column, but you do need to include it (along with "DISTINCT
") as the argument to "COUNT
":
SELECT COUNT(DISTINCT genre) AS total_genres FROM books;
Chandlerj Jones
18,378 PointsWhen answering this, the submission requires you to include "GROUP BY". When I attempted my solution using your suggestions I received: "Bummer: You're missing the GROUP BY keywords."
Steven Parker
231,269 PointsYou may be confusing this with a different exercise. You don't need grouping to get a count of all the genres, but you would to get the count of books in each genre.
HIDAYATULLAH ARGHANDABI
21,058 PointsIn 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. Get Help
SELECT distinct genre, count(*) as total_genres FROM books group by genre;
HIDAYATULLAH ARGHANDABI
21,058 Pointsthanks Steven
Matthew Lang
13,483 PointsMatthew Lang
13,483 PointsYour SQL looks perfectly valid, if that's what you're asking. I can't imagine you're asking how to solve the question though, since you never actually included the question at hand.