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 2: Advanced Selecting Predicting the Future

Ken Stone
Ken Stone
29,703 Points

I see some sixth grade classes with a max capacity of 15.

I see some sixth grade classes with a max capacity of 15. Using this query:

select count(*), room_id, rooms.capacity, period_id
from rooms
join classes on classes.room_id = rooms.id
join schedule on schedule.class_id = classes.id
join students on students.id = schedule.student_id
where students.grade = 6
group by room_id, period_id
order by capacity asc;

What is not right with this query?

1 Answer

Ben Deitch
Ben Deitch
Treehouse Teacher

Hey Ken!

To get which rooms teach 6th grade classes we join to the SUBJECTS table and use the GRADE column. You're using the STUDENTS.GRADE column, which means that any class that contains 6th graders will be included. So in your result set, you end up including the elective classes which shouldn't be counted as they have a GRADE of NULL in the SUBJECTS table.

Hope that helps!

Ken Stone
Ken Stone
29,703 Points

Thanks Ben, crystal clear on that now! Actually that was my thought process to find any class that has sixth graders in it. OK thanks again!