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 trialxczurymqdj
2,289 PointsIn 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.
6 Answers
Jason Larson
8,359 PointsIt'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
xczurymqdj
2,289 Pointsits 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;
Jason Larson
8,359 PointsIt 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;
xczurymqdj
2,289 PointsThank 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?
Jason Larson
8,359 PointsI'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.
xczurymqdj
2,289 PointsYes thank you for your help once again.