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 Querying Relational Databases Set Operations Union Operations

How to reset the MakeID with the correct order ID?

Select ForeignMakeID As ID, MakeName As GlobalMake From ForeignMake Union Select MakeID, MakeName From Make;

This will return all Foreign and Domestic Make but the ID has some duplicate number.

Is it possible to set the MakeID in the correct order without having any duplicate?

2 Answers

Steven Parker
Steven Parker
231,269 Points

From the database perspective, these aren't duplicates because either the name or number is different. If you want to display all the make names without duplicates, just leave off the id number:

SELECT MakeName AS GlobalMake FROM ForeignMake UNION SELECT MakeName FROM Make;

But with a subquery, you could combine the ID's into one column and use grouping to eliminate the duplicates:

SELECT group_concat(ID, ", ") AS IDs, GlobalMake FROM
(SELECT ForeignMakeID||"F" AS ID, MakeName AS GlobalMake FROM ForeignMake
 UNION ALL SELECT MakeID||"M", MakeName FROM Make)
GROUP BY GlobalMake

Since the ID's are associated with a particular table, I concatenated a letter onto each one to identify which table it applies to.

Awesome!