Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

There seem to be a lot of developers who write their SQL by capitalising the keywords:

SELECT column
FROM table
     INNER JOIN table
     ON condition
WHERE condition
GROUP BY clause
HAVING condition

I'm wondering why people stick to this approach? Clearly, it's a long established convention - but I've never run into a RDBMS that requires capitalisation.

Personally, I find KEYWORDS THAT SHOUT to be calling attention to exactly the wrong part of the query, which is why I write the keywords in lowercase.

Still, enough people use this convention that I figure I might be missing something, hence this question.

share|improve this question
3  
Nice to have the keywords stand out more, even if your IDE makes keyword text a different color. –  The Muffin Man Aug 15 '11 at 21:53
2  
The only way to cope with having a Language within a Language, is to SHOUT the commands at the SQL interpreter ... –  Ben DeMott Aug 15 '11 at 21:57
    
My question too. +1 –  Saeed Neamati Aug 16 '11 at 13:34
    
Capitalizing SQL keywords I can understand. Most table declarations and queries I see capitalize EVERYTHING, table names and columns included. –  MPelletier Sep 26 '12 at 14:02
add comment

5 Answers

up vote 13 down vote accepted

Capitalization makes them stand out, as opposed to the other characters that are in the query window.

The reason I don't do this as it's a huge time waster. You can do one of two things:

1) Hold your shift key down while typing out the word: way too error-prone and just haphazard.

2) Put on caps lock for the duration of the word: a bit too much of time consumption.

I use SQL Server, and the environment (SSMS) has great syntax highlighting so I don't personally believe keyword capitalization is as prevalent these days as it used to be (if at all).

It is good practice in books and online tutorials, though, so it is evident what the reserved keywords are. It's just one of those unwritten things.

share|improve this answer
5  
+1 syntax highlighting makes capitalisation redundant. –  dave Aug 15 '11 at 22:02
11  
@dave but unlike application code, SQL scripts are very likely to be viewed in an editor without syntax highlighting. –  Aaronaught Aug 15 '11 at 23:50
6  
@dave: SQL scripts are completely self-contained and almost invariably get e-mailed, typed into textareas, printed out for auditors, or input into command-line utilities like SQL*Plus. This isn't about your environment, it's about the lifetime of SQL scripts outside that environment - something that isn't really a concern with application source code because it has no real use outside of a compiler/IDE. –  Aaronaught Aug 16 '11 at 2:45
6  
SQL statements frequently appear in log files, error message, embedded in XML documents and all sorts of places where syntax highlighting is not an option - so any formatting which make it easier to read is always welcome. –  James Anderson Aug 16 '11 at 3:56
4  
-1 if holding down shift or pressing caps lock is too much then I think there are other problems. And exactly how much does that .25 seconds you saved actually affect anything? –  TheLQ Aug 16 '11 at 6:13
show 6 more comments

Personally, I find KEYWORDS THAT SHOUT to be calling attention to exactly the wrong part of the query, which is why I write the keywords in lowercase.

As has been pointed out, with old IDE's you didn't have many options besides casing to differentiate text. As you point out, making the keywords stand out draws your attention away from where it should be. The issue is that you know keywords can always be capitalized, whereas the other stuff can contain literals and identifiers that are case sensitive. This lead to the path of least resistance being to just capitalize the keywords.

share|improve this answer
add comment

SQL is a clause-oriented language, much more than anything else I've experienced. You've got your SELECT clause, and your FROM clause, and your JOIN clause(s) and your WHERE clause. ORDER BY, GROUP BY, etc, etc.

Capitalizing those keywords helps you visually separate the separate clauses. This is especially handy when in one of those tricky debugging situations where you're outputting your SQL in an HTML comment, then copy-pasting into a console. Nasty, but we've all been there. It's good to have some help seeing where each piece of the statement starts.

share|improve this answer
add comment

Well back in the olden days, keywords weren't highlighted in any way when you wrote code so it was an attempt to help people differentiate between the SQL parts and the database objects parts of the queries, it could have gone the other way but objects tend to have longer names making them more of a pain to capitalize and some object naming conventions were case specific.

Personally I find it just as easy to read if they aren't capitalized but many people are so used to reading them that way, it throws them off if you don't capitalize.

share|improve this answer
2  
I've recently been exposed to people who actually do reverse the convention, capitalizing the object names and leaving the keywords in lowercase; it's jarring, to say the least. –  Aaronaught Aug 15 '11 at 23:51
add comment

I find this sort of capitalization provides a quick and easy way to visually break up the different parts of the query. This combined with good indentation can help the readability and understanding of the logic.

Whether or not that has anything to with how the standard came about, I don't know. But that's my personal experience.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.