Random values

  • I'm with Jeff, Steve and Hugo. I hate it when I see a large column list and the full table name is on every single column. The two-part table name in the FROM clause should be aliased with something that makes sense (e.g.: FROM Customers c) and the alias should be used in the column list. It's a matter of readability.

    Then again, I also have my JOINs start a new line instead of trailing the line before it. Production code also has to be formatted with proper indenting. After all "It ain't done until it's pretty." And I always terminate my statements with a semicolon.

  • edwardwill (9/29/2014)


    Hugo Kornelis (9/26/2014)


    ... my personal coding standard is to always use aliases for all tables in queries that use two or more tables, but use no alias in single-query tables - so for those queries I have to manually remove the alias that SQL Prompt adds.

    I'm interested to know how or why you came up with this coding standard (I did my degree dissertation on coding standards). My coding standard is never to alias tables (at least not in databases that I designed, because I use descriptive, self-commenting table names) unless it's absolutely essential (where a table needs to be referenced more than once in a query).

    Others have already responded, but you asked me directly so I'll answer directly.

    In most real databases, table names tend to get long. (The alternative is that they get cryptic, which is even worse). Repeating the table name a zillion times in each query makes the code unnecessary hard to read. Plus I also notice that people tend to only include table names when needed when the table names are long, and are much more inclined to always include the alias when it's a short code.

    The alias should always be a mneomonic. When possible, I try to use the same mnemonic for each table every time I alias it. So I choose my mnemonics to not clash on tables that are frequently used in the same query. Rarely, I do have a clash, when two tables that normally are part of disparate areas of the system suddenly do have to appear in the same query - in that case I will add a distinguishing (and again mnemonic) letter to either or both tables.

    Or the short version: I consider code that uses short, mnemonic aliases and that qualifies every column with that alias to be more readable than code that qualifies columns with full table names, or that does not qualify (all) columns.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Good question, thanks!

Viewing 3 posts - 46 through 47 (of 47 total)

You must be logged in to reply to this topic. Login to reply