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 trialKarenina Braun
6,474 PointsStuck! How do I join three SQL tables to answer these two questions? Or do I not need to?
I'm fairly certain I'll need to join three SQL tables to answer these two questions, but I can't figure it out! Question 1 has me completely stumped, but I think I'm close with question 2. It's important to keep in mind that this database doesn't actually exist, only in theory.
Both Question 1 and 2 use the same tables:
Classes
ID
Name
Students
ID
Name
ClassesStudents
ClassID
StudentID
Question 1: Write a SQL query that will return the name of each class and how many students are taking it.
Question 2: Write a SQL query that will return the names of classes that the student named "John" is taking. Assume there is only one student with that name in the database.
My guess on Question 1 (WIP):*
SELECT ClassesStudents.StudentID, ClassesStudents.ClassID, Classes.Name FROM ClassesStudents, Classes;
*My guess on Question 2:
SELECT Classes.Name FROM Students JOIN ClassesStudents ON ClassesStudents.StudentID=Students.ID AND Students.Name = "John" JOIN Classes ON Classes.ID=ClassesStudents.ClassID
Can anyone please help me out? I've googled everything I could think of :/ If anyone could answer these two questions for me, that would be amazing. I learn by dissecting!
1 Answer
Steven Parker
231,236 PointsIt would be easier to provide a specific answer if you give a link to the course page you're working on.
But in the meantime the "how many" part of the first question is a clue that you'll need an aggregate function to create this as there's no column that has that information already. This might also be a good opportunity to use grouping.