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.

Databases Reporting with SQL Aggregate and Numeric Functions Counting Groups

NOT understanding

SELECT COUNT genre FROM books GROUP BY genre_count;

2 Answers

Tomas Schaffer
Tomas Schaffer
11,594 Points

SELECT COUNT(genre) genre_count FROM books GROUP BY genre_count;

But what is the reason to group the results of one column? Because this query is the result of one record.

This Query counts the number of result.

SELECT COUNT(*) genre_count FROM books;

So this is the same with better performance because you dont select all data just counting rows of table:

SELECT COUNT(0) genre_count FROM books;

Steven Parker
Steven Parker
217,602 Points

Did you actually try any of those as answers to the challenge? :see_no_evil:

Tomas Schaffer
Tomas Schaffer
11,594 Points

You didn't put the link to challenge. I didn't even know thats from challenge. But i work with PostgreSQL. So post the link to challenge if you like to.

Steven Parker
Steven Parker
217,602 Points

The link to the challenge is the colored button in the upper right of this page that says "View Challenge".
The system put it there automatically when Derek created this question.

Tomas Schaffer
Tomas Schaffer
11,594 Points

OK I am sorry didnt se the button.This is the correct answer.

SELECT genre, count(0) as genre_count FROM books GROUP BY genre;

Tomas Schaffer
Tomas Schaffer
11,594 Points

I didn't see first time button to Challenge and didn't see whole question. The right Answer to Challenge is below Derek.

SELECT genre, count(0) as genre_count FROM books GROUP BY genre;