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 Getting the Grand Total

Totally lost me in this video...

Andrew completely lost me at the HAVING part, like why we need to use it as a conditional instead of WHERE. Sounds like some kind of operation error that SQL can't handle, but I don't know..

2 Answers

If you try to use a WHERE clause when using the SUM aggregate function, it throws an error because the aggregation happens in the Group By clause, and WHERE happens before the Group By. In this case, you have to use HAVING on the Group By to filter on conditions.

Why wouldn't aggregation still be able to happen after the result set is filtered by the WHERE clause? Jason Larson . I'm not exactly sure why it's throwing an error still, and why we need a unique keyword for it. You would think GROUP BY might still be able to run after WHERE filters the available rows, so I'm not sure why it's not able to function like this