Is there a good reason to use upper case for SQL keywords?

Viewed 58764

The default seems to be upper case, but is there really any reason to use upper case for keywords?

I started using upper case, because I was just trying to match what SQL Server gives me whenever I tried to create something, like a new stored procedure. But then, I felt terrible for my baby (5th) finger, that always needs to hold down the Shift button, so I stopped using upper case. Is there a reason why I should go back to upper case?

17 Answers

PERSONALLY, I DON'T LIKE MY SQL YELLING AT ME. IT REMINDS ME OF BASIC OR COBOL.

So I prefer my T-SQL lowercase with database object names MixedCase.

It is much easier to read, and literals and comments stand out.

It's just a matter of style, probably originating in the days when editors didn't do code colouring.

I used to prefer all upper case, but I'm now leaning towards all lower.

Either way, be consistent.

Gordon Bell's examples are not exactly correct; generally, only the keywords are highlighted, not the entire query. His second example would look like:

SELECT name, id, xtype, uid, info, status, 
base_schema_ver, replinfo, parent_obj, crdate, 
ftcatid, schema_ver, stats_schema_ver, type, 
userstat, sysstat, indexdel, refdate, version, 
deltrig, instrig, updtrig, seltrig, category, cache
FROM sysobjects
WHERE category = 0
AND xtype IN ('U', 'P', 'FN', 'IF', 'TF')
ORDER BY 1

I find this far easier to read, since the keywords stand out more. Even with syntax highlighting, I find the uncapitalized example much harder to read.

At my company, we go a little bit farther with our SQL formatting.

SELECT      name, id, xtype, uid, info, status, 
            base_schema_ver, replinfo, parent_obj, crdate, 
            ftcatid, schema_ver, stats_schema_ver, type, 
            userstat, sysstat, indexdel, refdate, version, 
            deltrig, instrig, updtrig, seltrig, category, cache
FROM sysobjects
LEFT JOIN systhingies ON
    sysobjects.col1=systhingies.col2
WHERE category = 0
    AND xtype IN ('U', 'P', 'FN', 'IF', 'TF')
ORDER BY 1

Upper case can provide a gain in keyword visibility, but you can compensate with code highlight and indentation.
We use lower case because query editor and other tools do wonders in editing t-sql code, and we see no need to torture the little finger.

Monkey see, monkey do for me. Pattern matching - if I do it the way I've seen it done, the structure of the clauses lines up mentally more easily.

Uppercase is less readable. The outline of all words are shaped like boxes; there are no descenders or ascenders. Lowercase FTW!

I find it more readable. Same for having a newline for the beginning of each clause and indenting between clauses.

Try a formatting product (I use SQL Prompt/SQL Refactor from Red Gate). You can set how you want the capitalization to work, and your code will always be consistently formatted. Rest your pinky and let the computer do the work for you.

One of the reasons for continuing to use capitalization is when you(or someone else) are viewing code in something like notepad, it makes it easier to read. i.e. you can differentiate easily between the "keywords" and the tablenames, SP's, udf's etc

Other than conformity for conformitys sake, no. Although it's a very subjective topic, I prefer using mixed case for all SQL. The SQL is much easier to read, and nothing is lost in modern IDEs where keywords are all color-coded anyway.

Related