28

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.

  • 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
  • 3
    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
22

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.

  • 20
    @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
  • 8
    @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
  • 8
    -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
  • 2
    My answer to your question is another question: do you write SQL code daily? And multiply .25 seconds by the amount of code a SQL developer/dba writes... – user29981 Aug 16 '11 at 10:35
8

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.

7

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.

  • 4
    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
6

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.

2

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.

-1

In my case, I prefer to capitalize the keywords, independently of syntax highlighting. Imagine that you don't have a proper SQL editor and you need to write/edit or open your scripts in Notepad or WordPad. These editors doesn't have a syntax highlighting.

I don't pay much atention to the syntax highlighting of SQL Server for the same reason that I described above, but it's useful (it's bored for me to see all the code in black). I have always used uppercase to distinguish keywords from my variables, constants, names, etcetera.

  • This should have been a comment, but you don't have the reputation to post one... – Adam Zuckerman Aug 3 '16 at 3:48
  • I covered this in the original question - using CAPITALS for the keywords attracts the readers attention to the least important part of the query or statement. – Bevan Aug 5 '16 at 20:41

protected by gnat Aug 3 '16 at 6:08

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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