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

HIDAYATULLAH ARGHANDABI
HIDAYATULLAH ARGHANDABI
21,058 Points

Is 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

Matthew Lang
Matthew Lang
13,483 Points

Your 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.

3 Answers

Steven Parker
Steven Parker
229,644 Points

Now 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
HIDAYATULLAH ARGHANDABI
21,058 Points

SELECT distinct genre, count(*) as total_genres FROM books;

is this what u mean sir

Steven Parker
Steven Parker
229,644 Points

As 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
Chandlerj Jones
18,378 Points

When 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
Steven Parker
229,644 Points

You 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
HIDAYATULLAH ARGHANDABI
21,058 Points

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. Get Help

SELECT distinct genre, count(*) as total_genres FROM books group by genre;