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 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,932 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