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 trialJosh Bennett
15,258 Pointshow does adding the aliases here make the code "cleaner"? it works the same without the additional mk. and md.
SELECT mk.MakeName, md.ModelName FROM make AS MK
INNER JOIN model AS md
ON mk.MakeID = md.MakeID;
produces the same result as
SELECT MakeName, ModelName FROM make AS MK
INNER JOIN model AS md
ON mk.MakeID = md.MakeID;
and
SELECT mk.MakeName, md.ModelName FROM make
INNER JOIN model
ON make.MakeID = model.MakeID;
so what gives??
1 Answer
Steven Parker
231,271 PointsThis might not be the best example for showing the advantages of aliases.
First off, your samples don't look like they would work, as they both contain references to aliases that are not defined.
But I'll assume those are just typo's and I get your point — there's not much difference in the code and no difference at all in the output when using the aliases.
Cases where using aliases are more advantageous would include:
- when the joined tables have columns with the same names, which would otherwise be ambiguous
- when the table names are much longer than the chosen aliases
- when the number of selected columns is very large and the source table for each is not obvious