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 SQL Reporting by Example Day 3: Getting Good at Grouping Busiest Teachers

Travis Williams
Travis Williams
5,786 Points

When was the HAVING keyword covered in the Beginning SQL Learning Track?

This lesson is near the end of the current Beginning SQL learning track, and I don't remember ever encountering the HAVING keyword. Was this covered in the track?

I went back through my notes and the SQL Cheatsheets on GitHub and found 1 instance of it in the Reporting With SQL cheatsheet but I don't remember it being used in a lesson and there wasn't much of an explanation of it on the GitHub page.

I worked out my own solution using a CTE since those were covered in the learning track.

WITH classes_taught AS(
  SELECT * FROM CLASSES
  JOIN TEACHERS ON CLASSES.TEACHER_ID = TEACHERS.ID 
  GROUP BY TEACHER_ID
  )
SELECT FIRST_NAME, LAST_NAME, PERIOD_ID AS 'Count of Classes' FROM classes_taught
WHERE PERIOD_ID = 7;

It can be frustrating as a student to see the solution presented with new material instead of building on what was already covered, but I understand that there are multiple tracks and lessons can belong to many learning areas.

I still think this is an amazing resource and is staffed and supported by a great community. Thank you!

2 Answers

Steven Parker
Steven Parker
244,190 Points

I don't remember where it might be introduced in the videos, but HAVING functions similarly to WHERE but it acts on summarized groups (as created by GROUP BY) instead of individual rows. It can simplify the query, often eliminating the need for a sub-query.

Laura Coronel
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Laura Coronel
Treehouse Teacher

Hi Travis Williams

You're right that HAVING is easy to miss. It's in the track, but only briefly: it comes up in Reporting with SQL, the third course in Beginning SQL, in the Aggregate and Numeric Functions stage. Go to the video Getting the Grand Total, at about the 4:15 mark. It also gets a quick mention in that stage's Practice Session. It's only a minute or so of a longer video, so it's no surprise it didn't stick. We'll pass your feedback on to the team.

The short version: HAVING is WHERE for groups. WHERE filters individual rows before they're grouped. HAVING filters the groups after GROUP BY, so you can use aggregate functions like COUNT() in the condition:

SELECT TEACHERS.ID, FIRST_NAME, LAST_NAME, COUNT(*) AS 'Count of Classes'
FROM TEACHERS
JOIN CLASSES ON TEACHERS.ID = CLASSES.TEACHER_ID
GROUP BY TEACHERS.ID
HAVING COUNT(*) = 7;

Using a CTE was a creative way to build on what you'd already learned! One thing to watch out for, though: your query never actually counts the classes. When you SELECT * with GROUP BY TEACHER_ID, SQLite keeps just one row from each teacher's group, so PERIOD_ID is the period of whichever class it happened to keep, not a count. WHERE PERIOD_ID = 7 keeps teachers whose kept row happens to be 7th period. That may match the expected answer on this data, but it's not guaranteed to.

Happy querying!