<p>What are the pros and cons of using table aliases in SQL?  I personally try to avoid them, as I think they make the code less readable (especially when reading through large where/and statements), but I'd be interested in hearing any counter-points to this.  When is it generally a good idea to use table aliases, and do you have any preferred formats?</p>

[{'answer_id': 11049, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 5, 'selected': False, 'text': "<p>Well, there are some cases you <em>must</em> use them, like when you need to join to the same table twice in one query.</p>\n\n<p>It also depends on wether you have unique column names across tables. In our legacy database we have 3-letter prefixes for all columns, stemming from an abbreviated form from the table, simply because one ancient database system we were once compatible with didn't support table aliases all that well.</p>\n\n<p>If you have column names that occur in more than one table, specifying the table name as part of the column reference is a must, and thus a table alias will allow for a shorter syntax.</p>\n"}, {'answer_id': 11050, 'author': 'Adam Lerman', 'author_id': 673, 'author_profile': 'https://Stackoverflow.com/users/673', 'pm_score': 1, 'selected': False, 'text': "<p>IMHO, it doesn't really matter with short table names that make sense, I have on occasion worked on databases where the table name could be something like VWRECOFLY or some other random string (dictated by company policy) that really represents users, so in that case I find aliases really help to make the code FAR more readable. (users.username makes a lot more sence then VWRECOFLY.username)</p>\n"}, {'answer_id': 11053, 'author': 'Rob Allen', 'author_id': 149, 'author_profile': 'https://Stackoverflow.com/users/149', 'pm_score': 6, 'selected': True, 'text': "<p>Table aliases are a necessary evil when dealing with highly normalized schemas. For example, and I'm not the architect on this DB so bear with me, it can take 7 joins in order to get a clean and complete record back which includes a person's name, address, phone number and company affiliation. </p>\n\n<p>Rather than the somewhat standard single character aliases, I tend to favor short word aliases so the above example's SQL ends up looking like: </p>\n\n<pre><code>select person.FirstName\n      ,person.LastName\n      ,addr.StreetAddress\n      ,addr.City\n      ,addr.State\n      ,addr.Zip\n      ,phone.PhoneNumber\n      ,company.CompanyName\nfrom tblPeople person\nleft outer join tblAffiliations affl on affl.personID = person.personID\nleft outer join tblCompany company on company.companyID = affl.companyID\n</code></pre>\n\n<p>... etc</p>\n"}, {'answer_id': 11056, 'author': 'mbillard', 'author_id': 810, 'author_profile': 'https://Stackoverflow.com/users/810', 'pm_score': 1, 'selected': False, 'text': "<p>I like long explicit table names (it's not uncommon to be more than 100 characters) because I use many tables and if the names aren't explicit, I might get confused as to what each table stores.</p>\n\n<p>So when I write a query, I tend to use shorter aliases that make sense within the scope of the query and that makes the code much more readable.</p>\n"}, {'answer_id': 11057, 'author': 'Shawn', 'author_id': 26, 'author_profile': 'https://Stackoverflow.com/users/26', 'pm_score': 2, 'selected': False, 'text': "<p>You need them if you're going to join a table to itself, or if you use the column again in a subquery...</p>\n"}, {'answer_id': 11064, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 3, 'selected': False, 'text': "<p>Microsoft SQL's query optimiser benefits from using either fully qualified names or aliases.</p>\n\n<p>Personally I prefer aliases, and unless I have a lot of tables they tend to be single letter ones.</p>\n\n<pre><code>--seems pretty readable to me ;-)\nselect a.Text\nfrom Question q\n    inner join Answer a\n        on a.QuestionId = q.QuestionId\n</code></pre>\n\n<p>There's also a practical limit on how long a Sql string can be executed - aliases make this limit easier to avoid.</p>\n"}, {'answer_id': 11072, 'author': 'BTB', 'author_id': 961, 'author_profile': 'https://Stackoverflow.com/users/961', 'pm_score': 3, 'selected': False, 'text': '<p><p>If I write a query myself (by typing into the editor and not using a designer) I always use aliases for the table name just so I only have to type the full table name once.</p>I really hate reading queries generated by a designer with the full table name as a prefix to every column name. </p>\n'}, {'answer_id': 11121, 'author': 'Stu', 'author_id': 414, 'author_profile': 'https://Stackoverflow.com/users/414', 'pm_score': 0, 'selected': False, 'text': "<p>I always use aliases, since to get proper performance on MSSQL you need to prefix with schema at all times. So you'll see a lot of</p>\n\n<blockquote>\n  <p>Select\n    Person.Name \n  From<br>\n    dbo.Person As Person</p>\n</blockquote>\n"}, {'answer_id': 12403, 'author': 'Juha Syrjälä', 'author_id': 1431, 'author_profile': 'https://Stackoverflow.com/users/1431', 'pm_score': -1, 'selected': False, 'text': '<p>Aliases are required when joining tables with columns that have identical names.</p>\n'}, {'answer_id': 14394, 'author': 'Craig', 'author_id': 1073, 'author_profile': 'https://Stackoverflow.com/users/1073', 'pm_score': 0, 'selected': False, 'text': '<p>I always use aliases when writing queries.  Generally I try and abbreviate the table name to 1 or 2 representative letters.  So Users becomes u and debtor_transactions becomes dt etc...</p>\n\n<p>It saves on typing and still carries some meaning.  </p>\n\n<p>The shorter names makes it more readable to me as well.</p>\n'}, {'answer_id': 14406, 'author': 'Sören Kuklau', 'author_id': 1600, 'author_profile': 'https://Stackoverflow.com/users/1600', 'pm_score': 3, 'selected': False, 'text': "<p>I suppose the only thing that really speaks against them is excessive abstraction. If you will have a good idea what the alias refers to (good naming helps; 'a', 'b', 'c' can be quite problematic especially when you're reading the statement months or years later), I see nothing wrong with aliasing.</p>\n\n<p>As others have said, joins <em>require</em> them if you're using the same table (or view) multiple times, but even outside that situation, an alias can serve to clarify a data source's purpose in a particular context. In the alias's name, try to answer <em>why</em> you are accessing particular data, not <em>what</em> the data is.</p>\n"}, {'answer_id': 15160, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>I always use aliases in my queries and it is part of the code guidebook in my company.  First of all you need aliases or table names when there are columns with identical names in the joining tables.  In my opinion the aliases improve readability in complex queries and allow me to see quickly the location of each columns.  We even use aliases with single table queries, because experience has shown that single table queries don´t stay single table for long.</p>\n'}, {'answer_id': 26616, 'author': 'Pulsehead', 'author_id': 2156, 'author_profile': 'https://Stackoverflow.com/users/2156', 'pm_score': 2, 'selected': False, 'text': "<p>Aliases are great if you consider that my organization has table names like:\nSchemaName.DataPointName_SubPoint_Sub-SubPoint_Sub-Sub-SubPoint...\nMy team uses a pretty standard set of abbreviations, so the guesswork is minimized.  We'll have say ProgramInformationDataPoint shortened to pidp, and submissions to just sub.</p>\n\n<p>The good thing is that once you get going in this manner and people agree with it, it makes those HAYUGE files just a little smaller and easier to manage.  At least for me, fewer characters to convey the same info seems to go a little easier on my brain.</p>\n"}, {'answer_id': 202162, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>If you do not use an alias, it's a bug in your code just waiting to happen.</p>\n\n<pre><code>SELECT Description -- actually in a\n FROM\n table_a a,\n table_b b\n WHERE\n a.ID = b.ID\n</code></pre>\n\n<p>What happens when you do a little thing like add a column called Description to Table_B. That's right, you'll get an error. Adding a column doesn't need to break anything. I never see writing good code, bug free code, as a necessary evil.</p>\n"}, {'answer_id': 265977, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<h1>Good</h1>\n\n<p>As it has been mentioned multiple times before, it is a good practice to prefix <em>all</em> column names to easily see which column belongs to which table - and aliases are shorter than full table names so the query is easier to read and thus understand. If you use a good aliasing scheme of course.</p>\n\n<p>And if you create or read the code of an application, which uses externally stored or dynamically generated table names, then without aliases it is really hard to tell at the first glance what all those "%s"es or other placeholders stand for. It is not an extreme case, for example many web apps allow to customize the table name prefix at installation time.</p>\n'}, {'answer_id': 6299649, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 5, 'selected': False, 'text': "<p>Am I the only person here who really hates them?</p>\n\n<p>Generally, I don't use them unless I have to. I just really hate having to read something like</p>\n\n<pre><code>select a.id, a.region, a.firstname, a.blah, b.yadda, b.huminahumina, c.crap\nfrom table toys as a\ninner join prices as b on a.blah = b.yadda\ninner join customers as c on c.crap = something else\netc\n</code></pre>\n\n<p>When I read SQL, I like to know exactly what I'm selecting when I read it; aliases actually confuse me more because I've got to slog through lines of columns before I actually get to the table name, which generally represents information about the data that the alias doesn't. Perhaps it's okay if you made the aliases, but I commonly read questions on StackOverflow with code that seems to use aliases for no good reason. (Additionally, sometimes, someone will create an alias in a statement and just not use it. Why?)</p>\n\n<p>I think that table aliases are used so much because a lot of people are averse to typing. I don't think that's a good excuse, though. That excuse is the reason we end up with terrible variable naming, terrible function acronyms, bad code...I would take the time to type out the full name. I'm a quick typer, though, so maybe that has something to do with it. (Maybe in the future, when I've got carpal tunnel, I'll reconsider my opinion on aliases. :P ) I especially hate running across table aliases in PHP code, where I believe there's absolutely no reason to have to do that - you've only got to type it once!</p>\n\n<p>I always use column qualifiers in my statements, but I'm not averse to typing a lot, so I will gladly type the full name multiple times. (Granted, I do abuse MySQL's tab completion.) Unless it's a situation where I have to use an alias (like some described in other answers), I find the extra layer of abstraction cumbersome and unnecessary.</p>\n\n<p><strong>Edit:</strong> (Over a year later) I'm dealing with some stored procedures that use aliases (I did not write them and I'm new to this project), and they're kind of painful. I realize that the reason I don't like aliases is because of how they're defined. You know how it's generally good practice to declare variables at the top of your scope? (And usually at the beginning of a line?) Aliases in SQL don't follow this convention, which makes me grind my teeth. Thus, I have to search the entire code for a single alias to find out where it is (and what's frustrating is, I have to read through the logic before I find the alias declaration).  If it weren't for that, I honestly might like the system better.</p>\n\n<p>If I ever write a stored procedure that someone else will have to deal with, I'm putting my alias definitions in a comment block at the beginning of the file, as a reference. I honestly can't understand how you guys don't go crazy without it.</p>\n"}, {'answer_id': 13294802, 'author': 'Kirk Harrington', 'author_id': 1810120, 'author_profile': 'https://Stackoverflow.com/users/1810120', 'pm_score': 2, 'selected': False, 'text': "<p>I LOVE aliases!!!!  I have done some tests using them vs. not and have seen some processing gains.  My guess is the processing gains would be higher when you're dealing with larger datasets and complex nested queries than without.  If I'm able to test this, I'll let you know.  </p>\n"}]
['https://Stackoverflow.com/questions/11043', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/637/']