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

Geovani Mejia
seal-mask
.a{fill-rule:evenodd;}techdegree
Geovani Mejia
Front End Web Development Techdegree Student 7,940 Points

Question on the GROUP BY keyword

HERE IS THE CHALLENGE: Which teachers teach a class during all 7 periods?

SELECT * FROM TEACHERS INNER JOIN CLASSES ON CLASSES.TEACHER_ID = TEACHERS.ID GROUP BY TEACHERS.ID;

CONFUSION:

I dont understand how the keyword GROUP BY TEACHERS.ID shows all the teachers teaching 7 periods. When you run the query, the PERIOD_ID COLUMN shows a 7. What does this 7 mean?

1 Answer

Steven Parker
Steven Parker
218,596 Points

It doesn't really mean anything in this query. Since the GROUP BY guarantees only one line per teacher, the value shown in that column is just the period number of the last entry for that teacher in the CLASSES table.

If you watch the entire lesson to the end, you'll see that an aggregate function is added to count up the classes taught by each teacher and then test that with a HAVING filter to be sure each teacher shown has all 7.