Why should I capitalize my SQL keywords?

SqlFormattingReadability

Sql Problem Overview


> Possible Duplicate:
> Is there a good reason to use upper case for T-SQL keywords?

Simple question. I personally find a string of lowercase characters to be more readable than a string of uppercase characters. Is some old/popular flavor of SQL case-sensitive or something?

For reference:

select
    this.Column1,
    case when this.Column2 is null then 0 else this.Column2 end
from dbo.SomeTable this
    inner join dbo.AnotherTable another on this.id = another.id
where
    this.Price > 100

vs.

SELECT
    this.Column1,
    CASE WHEN this.Column2 IS NULL THEN 0 ELSE this.Column2 END
FROM dbo.SomeTable this
    INNER JOIN dbo.AnotherTable another ON this.id = another.id
WHERE
    this.Price > 100

The former just seems so much more readable to me, but I see the latter way more often.

Sql Solutions


Solution 1 - Sql

I agree with you - to me, uppercase is just SHOUTING.

I let my IDE handle making keywords stand out, via syntax highlighting.

I don't know of a historical reason for it, but by now it's just a subjective preference.

Edit to further make clear my reasoning:

Would you uppercase your keywords in any other modern language? Made up example:

USING (EditForm form = NEW EditForm()) {
    IF (form.ShowDialog() == DialogResult.OK) {
       IF ( form.EditedThing == null ) {
          THROW NEW Exception("No thing!");
       }
       RETURN form.EditedThing;
    } ELSE {
       RETURN null;
    }
}              

Ugh!

Anyway, it's pretty clear from the votes which style is more popular, but I think we all agree that it's just a personal preference.

Solution 2 - Sql

I think the latter is more readable. You can easily separate the keywords from table and column names, etc.

Solution 3 - Sql

One thing I'll add to this which I haven't seen anyone bring up yet:

If you're using ad hoc SQL from within a programming language you'll have a lot of SQL inside strings. For example:

insertStatement = "INSERT INTO Customers (FirstName, LastName) VALUES ('Jane','Smith')"

In this case syntax coloring probably won't work so the uppercasing could be helping readability.

Solution 4 - Sql

From Joe Celko's "SQL Programming Style" (ISBN 978-0120887972):

> Rule: > > Uppercase the Reserved Words. > > Rationale: > > Uppercase words are seen as a unit, > rather than being read as a series of > syllables or letters. The eye is drawn > to them, and they act to announce a > statement or clause. That is why > headlines and warning signs work. > > Typographers use the term bouma for > the shape of a word. The term appears > in paul Saenger's book (1975). Imagine > each letter on a rectangular card that > just fits it, so that you see the > ascenders, descenders, and baseline > letters as various "Lego blocks" that > are snapped together to make a word. > > The bouma of an uppercase word is > always a simple, dense rectangle, and > it is easy to pick out of a field of > lowercase words.

What I find compelling is that this is the only book about SQL heuristics, written by a well-known author of SQL works. So is this the absolute truth? Who knows. It sounds reasonable enough and I can at least point out the rule to a team member and tell them to follow it (and if they want to blame anyone I give them Celko's email address :)

Solution 5 - Sql

Code has punctuation which SQL statements lack. There are dots and parentheses and semicolons to help you keep things separate. Code also has lines. Despite the fact that you can write a SQL statement on multiple physical lines, it is a single statement, a single "line of code."

IF i were to write english text without any of the normal punctuation IT might be easier if i uppercased the start of new clauses THAT way itd be easier to tell where one ended and the next began OTHERWISE a block of text this long would probably be very difficult to read NOT that id suggest its easy to read now BUT at least you can follow it i think

Solution 6 - Sql

Mostly it's tradition. We like to keep keywords and our namespace names separate for readability, and since in many DBMSes table and column names are case sensitive, we can't upper case them, so we upper case the keywords.

Solution 7 - Sql

I prefer lower case keywords. Management Studio color codes the keywords, so there is no problem distinguishing them from the identifiers.

And upper case keywords feels so... well... BASIC... ;)

-"BASIC, COBOL and FORTRAN called from the eighties, they wanted their UPPERCASE KEYWORDS back." ;)

Solution 8 - Sql

What's worse it that as the majority of developers at my office believe in capitals for sql keywords so I have had to change to uppercase. Majority rules.

I believe lowercase is easier to read and that given that sql keywords are highlighted in blue anyway.

In the glory days keywords were in captials because we were developing on green screens!

The question is: if we don't write c# keywords in uppercase then why do I have to write sql keywords in uppercase?

Like someone else has said - capitals are SHOUTING!

Solution 9 - Sql

I like to use upper case on SQL keywords. I think my mind skips over them as they are really blocky and concentrates on what's important. The blocky words split up the important bits when you layout like this:

SELECT
  s.name,
  m.eyes,
  m.foo
FROM
  muppets m,
  muppet_shows ms,
  shows s
WHERE
  m.name = 'Gonzo' AND
  m.muppetId = ms.muppetId AND
  ms.showId = s.showId

(The lack of ANSI joins is an issue for another question.)

There is a psychology study that shows lowercase was quicker to read than uppercase due to the outlines of the words being more distinctive. However, this effect can disappear about with lots of practice reading uppercase.

Solution 10 - Sql

Back in the 80s, I used to capitalize database names, and leave sql keywords in lower case. Most writers did the opposite, capitalizing the SQL keywords. Eventually, I started going along with the crowd.

Just in passing, I'll mention that, in most published code snippets in C, C++, or Java the language keywords are always in lower case, and upper case keywords may not even be recognized as such by some parsers. I don't see a good reason for using the opposite convention in SQL that you use in the programming language, even when the SQL is embedded in source code.

And I'm not defending the use of all caps for database names. It actually looks a little like "shouting". And there are better conventions, like using a few upper case letters in database names. (By "database names" I mean the names of schemas, schema objects like tables, and maybe a few other things.) Just because I did it in the 80s doesn't mean I have to defend it today.

Finally, "De gustibus non disputandum est".

Solution 11 - Sql

It's just a matter of readability. Helps you quickly distinguish SQL keywords.

Btw, that question was already answered: https://stackoverflow.com/questions/153944/is-sql-syntax-case-sensitive

Solution 12 - Sql

I prefer using upper case as well for keywords in SQL.

Yes lower case is more readable but for me having to take extra second to scan through the query will do you good most of the time. Once it's done and tested you should rarely ever see it again anyway (DAL, stored proc or whatever will hide it from you).

If you are reading it for the first time, capitalized WHERE AND JOIN will jump right at you, as they should.

Solution 13 - Sql

Its just a question of readability. Using UPPERCASE for the SQL keywords helps make the script more understandable.

Solution 14 - Sql

I capitalize SQL to make it more "contrasty" to the host language (mostly C# these days).

It's just a matter of preference and/or tradition really...

Solution 15 - Sql

Apropos of nothing perhaps, but I prefer typesetting SQL keywords in small caps. That way they look capitalized to most readers, but they aren't the same as the ugly ALL CAPS style.

A further advantage is that I can leave the code as is and print it in the traditional style. (I use the listings package in LaTeX for pretty-printing code.)

Solution 16 - Sql

Some SQL developers here like to lay it out like this:

SELECT s.name, m.eyes, m.foo
FROM muppets m, muppet_shows ms, shows s 
WHERE m.name = 'Gonzo' AND m.muppetId = ms.muppetId AND ms.showId = s.showId

They claim this is easier to read unlike your one field per line approach which I use myself.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSamantha BranhamView Question on Stackoverflow
Solution 1 - SqlBlorgbeardView Answer on Stackoverflow
Solution 2 - SqlTrentView Answer on Stackoverflow
Solution 3 - SqlUserView Answer on Stackoverflow
Solution 4 - SqlonedaywhenView Answer on Stackoverflow
Solution 5 - SqlInstance HunterView Answer on Stackoverflow
Solution 6 - SqlPaul TomblinView Answer on Stackoverflow
Solution 7 - SqlGuffaView Answer on Stackoverflow
Solution 8 - SqlSteveView Answer on Stackoverflow
Solution 9 - SqlWW.View Answer on Stackoverflow
Solution 10 - SqlWalter MittyView Answer on Stackoverflow
Solution 11 - SqlrogeriopvlView Answer on Stackoverflow
Solution 12 - SqljfrobishowView Answer on Stackoverflow
Solution 13 - Sqluser59634View Answer on Stackoverflow
Solution 14 - SqlBranko DimitrijevicView Answer on Stackoverflow
Solution 15 - SqlJon EricsonView Answer on Stackoverflow
Solution 16 - SqlSteveView Answer on Stackoverflow