Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
In need of some help again, My code is not working right. please help me.
I've tryed different way to wright this code but i keep getting theres somthing wrong with your sql code.
It's difficult to tell you what you're doing wrong without knowing which of the tasks you're working on and without seeing your code. I'm assuming this is the first task.
SELECT genre, COUNT(*) AS genre_count FROM books GROUP BY genre
its the second task and not sure why its not showing my stuff. Here's what the task is:
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.
my code was this: SELECT COUNT DISTINCT genre FROM books;
It looks like you almost got it right. Remember that you need parentheses around what you're counting, and the challenge also asks you to alias the column as total_genres.
SELECT COUNT(DISTINCT genre) AS total_genres FROM books;
Thank you, But there's one thing that im not understanding. How does the count get the distinct but it doesn't show you this in the lessons?
I'm not sure exactly what you mean. Count is simply a function that counts what you've selected. As an example, let's say there are 20 rows of data for books, and of the 20 books in the table, there are only 2 different genres ('fiction' and 'non-fiction'), and to make things easier to see, let's say that 15 are fiction and 5 are non-fiction. If you do
select genre from books, you'll get back 20 rows of data, with 15 of them saying 'fiction' and 5 saying 'non-fiction'. if you do
select count(*) from books where genre = 'fiction' you'll get back 15. If you do
select distinct genre from books you'll get back 2 rows of data, 'fiction' and 'non-fiction', so if you do
select count(distinct genre) from books you'll get back just the count of 2.
Andrew covers this in the first video at around 3:20 or so.
Hopefully this clears things up.