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 trialMichael Nanni
7,358 PointsDoes grade level matter to find which subject is least popular?
https://teamtreehouse.com/library/least-popular-subject-2
WITH CLASS_POPULATION AS (
SELECT COUNT(STUDENT_ID) AS CLASS_SIZE, CLASS_ID, SUBJECT_ID,
SUBJECTS.NAME AS SUBJECTNAME FROM STUDENTS
JOIN SCHEDULE ON SCHEDULE.STUDENT_ID = STUDENTS.ID
JOIN CLASSES ON CLASSES.ID = SCHEDULE.CLASS_ID
JOIN SUBJECTS ON CLASSES.SUBJECT_ID = SUBJECTS.ID
GROUP BY SUBJECTS.NAME
)
SELECT MIN(CLASS_SIZE), CLASS_POPULATION.SUBJECTNAME FROM CLASS_POPULATION;
I got the same answer as Ben, but had a very different approach. I started by getting all students regardless of grade and then joined out to the classes and subjects tables. Right before the initial query was converted to a CTE, the result set was very different. EX: There were around 512 students taking Math) but the same amount in Puppetry.
This is likely because the Puppetry class isn't restricted to grade level. So the question I have is, does grade level matter for this challenge? Is my code appropriate as-is?
I could see it being important if the report needed to be more granular, but that didn't seem to be what was asked here.
Thanks in advance for the feedback!
1 Answer
Steven Parker
231,248 PointsYou're right that the question asked for this exercise is not related to grade level.
And it's quite common for different approaches to constructing a query to lead to equally valid ways of creating the same result set. At that point, picking one way over another might be a matter of efficiency; but benchmarking performance is a topic for more advanced studies.