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 trialShan Liu
3,612 PointsDuplicates?
So in the case of where there are duplicate values like with the BMW or the Honda because of their id numbers being different, how would we exclude them? Would it be with a DISTINCT/GROUP BY combo that allows you to filter out the duplicates? or is there a different keyword that is being used for this certain circumstance?
2 Answers
Steven Parker
231,184 PointsEither DISTINCT
or GROUP BY
can be used to eliminate duplicates.
Generally, you'd use GROUP BY
if your output contained at least one aggregate value, and DISTINCT
otherwise.
But for this exercise, combining with UNION works, as it implies "distinct", just remember that it applies to all displayed columns. To get only distinct makes, that means you would display the MakeName but not the MakeId. If the MakeId is displayed, then the rows are not duplicates.
kevin hudson
Courses Plus Student 11,987 Points@Steven Parker I thought the same thing but DISTINCT and GROUP BY does nothing. When joining we see Make and ForeignMake tables info where BMW is listed twice but has different ID's. I am assuming that we are stuck with that unless we add another WHERE condition:
SELECT * FROM Make
WHERE MakeName < "D" AND MakeName != "BMW"
UNION
SELECT * FROM ForeignMake
WHERE MakeName < "D" AND MakeName != "BMW"
ORDER BY MakeName;
Steven Parker
231,184 PointsThe problem there is that "BMW" has been eliminated completely, and coding it that way requires prior knowledge of the contents of the database.
See the comment I added to my answer for an example of using GROUP BY.
Steven Parker
231,184 PointsSteven Parker
231,184 PointsThe catch in using GROUP BY is determining an aggregate method that makes sense for the data. Since the ID's come from separate sets, it doesn't really make sense to combine them. But you can still use GROUP BY to give you a single row for each make if you return the ID's in separate columns: