Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I capitalize my SQL keywords? [duplicate]

People also ask

Why are SQL keywords capitalized?

All Caps SQL CommandsFor readability, all SQL commands should be written in uppercase letters. This allows the reader to identify the keywords in the SQL statement and easily determine what the query is executing.

Should keywords capitalized?

Capitalize the first letter of keywords.If your keyword is a phrase, capitalize only the first letter of the first word, for example: Business administration. If your keyword is a proper name, capitalize the first letter of each word, for example: Mississippi River.

Does SQL care about capitalization?

Answer. No, SQLite, which Codecademy uses, is case-insensitive when it comes to clauses like SELECT and FROM which can be cased in any way. This is different from other programming languages such as Python where casing is quite important.

Why do you think the keywords in SQL commands written are capitalized does it affect the query in anyway Why?

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.


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.


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


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.


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 :)