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

Development Tools Database Foundations SQL Calculating, Aggregating and Other Functions String Functions

Bruno Calheira
Bruno Calheira
12,625 Points

When should I use HAVING in SQL Statements?

What's the difference between the "HAVING" and "WHERE" keywords in SQL Statements?

When should I use one or another in a SELECT?

2 Answers

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

The HAVING clause is equivalent of the WHERE clause when your queries are aggregate functions.

I mention the subtle differences in this video.

In the video (2:20) the HAVING clause is used on a SELECT statement

SELECT first_name, UPPER(last_name), LOWER(email), LENGTH(username) AS username_length FROM users HAVING username_length < 19;

How does that even compile? There are is no GROUP BY clause in the statement.