SQL Queries - Use The Alias!!

Back to my SQL theme for today. I'm looking at a query right now that is driving me nuts. Just a tip - If you're going to write a query that joins multiple tables together, it's really a good idea to use the table alias within the query itself - otherwise other people who read that query will have a pretty darn hard time tracking down which columns came from which tables.

For example - the bad way:

select something,somethingElse, lotsOfOtherSomethingElses
from tblOne as a
inner join tblTwo as b
on a.fooId = b.fooId
lots of other joins...

The better way:

select a.something,b.somethingElse, c.lotsOfOtherSomethingElses
from tblOne as a
inner join tblTwo as b
on a.fooId = b.fooId
lots of other joins...

You can get away with not aliasing the columns when the column names are not shared between multiple tables, but it's just so damn hard to read when you don't.



Comments
Well, you actually don't need aliases to do what you want. What you are asking for is fully qualified column names, and you can do that using aliases or using table names.

--- Ben
# Posted By Ben Forta | 12/4/06 3:28 PM
True... I guess in my rage I didn't quite think that out ;)

I do prefer aliases believe it or not - isn't that strange? Just seems more readable to me.
# Posted By todd sharp | 12/4/06 3:31 PM
Ben is correct (of Course!)

But I do agree with the post's idea. Heck, I give anything with a join and alias for simplicity now, and later.
# Posted By Kevin Sargent | 12/4/06 3:39 PM
Word up. Table aliasing is awesome. Even if you can do it without table aliasing, the use of the alias cuts down on the "not as useful" text that the programmer has to read and thereby increases the readability of the SQL query and its maintainability.
# Posted By Ben Nadel | 12/4/06 3:44 PM
ok first... what ben said.

with that out of the way, i'm going on record as saying i actually prefer to not have the table names aliased (but they should be fully qualified...see above). yes, i know it cuts down on typing. And it arguably makes the code more readable...at first.

i find when i'm maintaining complex queries that have aliased table names, i spend more time poring thru the JOINs trying to find the appropriate table name that corresponds to 'c' (or whatever your favorite letter of the alphabet is).

i generally try to be pretty descriptive in my naming conventions. i find that helps me during the ongoing maintenance phase of a project. a table alias is really just a variable. if I have a variable that's supposed to hold a user name, i'm probably going to call it username. if i have a table that's already named 'username' (and let's just say for the sake of argument that for some reason, i do have a table with this name), why obscure it by aliasing it with a single letter?

i know i'm likely in the minority here and i'm not saying that not using aliases is "better". i'm saying it's more comfortable for me. i guess that just makes me kinda weird :)

seriously...is it just me? :)
# Posted By charlie griefer | 12/4/06 3:55 PM
I agree with Charlie... sometimes. I think with a large complex query, the full table alias might be more readable (depending on the names of the original tables). That is to say, nothing is *best* for all situations.

Have you ever had a query that joins to the same table twice! That gets a bit strange and you have to come up with really descriptive aliasing so your head doesn't explode.

Bottom line, you have to maintain a delicate balance between readability and an extraneous effort.
# Posted By Ben Nadel | 12/4/06 3:58 PM
What I usually do whether using aliases or not is put each table's columns on different lines. This allows you to qualify the columns that need to be but not clutter up the rest.

SELECT Table1.Column1, Column2, Table1.Column3, Column4,
      Table2.Column1, Column2
      Table3. ETC ETC
# Posted By Dan Roberts | 12/4/06 4:00 PM
I'm with Charlie and Ben.

I hate aliases. How am I supposed to know what "sc" or "s" or "r" are supposed to mean? I prefer to use the fully qualifed tablename.column (as Ben said )
# Posted By Jeff Houser | 12/4/06 9:19 PM
I think it really comes down to preference and situation.

For simple queries, with 2 or 3 joins, I prefer an alias.

On more complex queries with huge field lists, lots of tables, group, etc.. fqtn probably make more sense. On my dinky little sites, the query is generally no longer than a few lines and without even trying, i can usually see the aliases.

you've (we've) got to do whatever is best for the situation.
# Posted By Kevin Sargent | 12/11/06 6:39 PM

Calendar

Sun Mon Tue Wed Thu Fri Sat
      1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30       

Subscribe

Enter your email address to subscribe to this blog.

Tags

actionscript ajax blogging cfsnippets coldfusion flash forms flex funny stuff misc model-glue off topic personal project learn slidesix sql

Recent Comments

Editing A Query In A SQL Server DTS Package
JD said: Thanks for your post. Never unlike Microsoft to hide stuff in the hardest part time find. [More]

Mashing Spry Effects With CF8 Ajax Goodness
Mark Pitts said: I have had moderate success implementing Spry Accordian. Sadly the part that does is not working wil... [More]

Chinese Birth Calendar Accuracy Test
Toni Lehman said: This calendar was accurate for both my daughters and 4 grandchildren. I tried it for 11 of my other ... [More]

Virtual Memory - Am I The Last To Know?
Larry Miller said: The authors friend was right. Windows virtual memory system was designed by experts and they fully u... [More]

Using A PlayStation 2 HDD In Your PC
Alacres said: Thanks so much for the guide man! I did have a more specific question though, since I didn't see it ... [More]

RSS


coldfusionbloggers

FullAsAGoog MXNA

Consumed By Feed-Squirrel.com