I always see examples this way, but why? Is this good practice?
So they're distinguishable from the rest of the query (which is typically written in upper case).
As for whether or not it's a best practice...if you're writing queries in all upper case, then yes it definitely makes your queries easier to read and understand.
I think just like casing questions with SQL-it's all personal preference. I like all lowercase in my queries so I tend to just keep it that way with aliases as well.
Convention is always a good practice, so it is good to follow what your dev team has agreed upon. Many people subscribe to putting keywords in UPPER case, so differentiating aliases from keywords by making them lower is common.
As said before, I think its personal preference.
I mostly use lower case, except aliases which I always capitalize.
I write queries only in stored procedures so I write only the important part of my query (and other TSQL "commands" like BEGIN, END, IF, ELSE, WHILE, etc.) in upper case use. All aliases are capitalized so I can see at a glance to which table an attribute belongs.
If someone joins my team (project) he has to do the same, as I do when I join someone else's project. As I try to make it more readable, I think that line breaks and indentations are more important that case (as long as it stays the same through the whole project).
I use lower case
for the names invented by me.
These are table names, column names, my function names, aliases, etc.
The upper case
is for the names invented by somebody else
That is reserved words, built-in functions, etc.
dual
and dummy
in Oracle
are notable exception from this rule, but they are table name and column name, so I just use like with like.
I think there are several reasons to write them in lowercase:
- Lowercase looks less like "shouting". Lots of UPPERCASE characters look like shouting (most people on a forum won't like posts in UPPERCASE). Some write keywords like IF in upper case, when also writing your aliases in uppercase you might get confused.
- If you start a tablename with a Capital and your aliases with lower case characters you can keep them apart. Otherwise you might get confused when there are also tables with shorter names.
But no matter what standard you use in a team as long as everybody is using the same rules you can read each other codes and it will look less messy. Sometimes code can be so complex you don't want te get distracted by code which violates the "rules".