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 of Rows

What if I use ORDER BY?

First I just want to state that I totally understand how GROUP BY works. But still, I want to know clearly why when I used ORDER BY like this:

SELECT category, COUNT(*) AS product_count FROM products ORDER BY category;

the result came up this way:

category_____________________products_count

Books________________________29

Why only 1 row showed up? Where are the two other rows Clothing and Electronics? I did not use any query to leave those two categories out. This really baffled me. Please help!

1 Answer

ivana kantnerova
ivana kantnerova
15,673 Points

because there is no group by category, the aggregation function "count (*)" counts all rows of the table => the result has only one row .. it displays only the first category only because there is no other rows in results ... agregational function always returns only one value