29

I have often seen the queries written in SQL in CAPITAL LETTERS. Though I think SQL is not case sensitive, why do most of them prefer to write in CAPITAL LETTERS ? Is it just a matter of choice or does it have some logic?

Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
saplingPro
  • 19,801
  • 53
  • 136
  • 190
  • 2
    possible duplicate of [Why are MySQL queries almost always written in Capital](http://stackoverflow.com/questions/9325927/why-are-mysql-queries-almost-always-written-in-capital) – Sudhir Bastakoti Feb 24 '12 at 06:00
  • 42
    IF YOU WRITE YOUR QUERIES LIKE THIS THE SERVER WILL HEAR THEM BETTER AND EXECUTE THEM BEFORE quieter queries, written like this. At least that's what I think, and why my queries are mostly written in capital letters. – ta.speot.is Feb 24 '12 at 06:02
  • 1
    possible duplicate of [Lowercase or uppercase for SQL Statements](http://stackoverflow.com/questions/7566164/lowercase-or-uppercase-for-sql-statements) – Druid Aug 01 '12 at 09:36
  • Given they are not needed and reading them is known to be harder to read - https://www.mity.com.au/blog/writing-readable-content-and-why-all-caps-is-so-hard-to-read - I would encourage ditching the ALLCAPS convention entirely. – Tim Abell Mar 04 '21 at 10:04

7 Answers7

45

It was meant for readability. Before, SQL was written in plain-text editors (no syntax/code highlighting) and keywords needed to be differentiated for better readability and maintenance.

SELECT column_name
FROM table_name
WHERE column_name = column_value

vs

select column_name from table_name where column_name = column_value

See the difference? The first word in each line told the reader exactly what was going on (selecting something from somewhere where something).

But now with syntax-highlighting, there's really no point. But it IS a best practice and allows SQL developers to be on the same page/style while developing.

Justice Erolin
  • 2,819
  • 19
  • 19
4

It varies with when you learned SQL and who or where you learned it from.

It originated on IBM mainframes and those definitely preferred upper-case.

These days, I write the keywords in all-caps and the database objects in either lower-case or in SuitablyMixedCase. That simply makes it easy to see the keywords, while leaving the important information (table names, column names, etc) in more readable mainly lower-case letters. It makes it trivial to read past the keywords.

Of course, I've been writing SQL for...ulp...over twenty-five years, so lots of stuff like this is simply 'second nature' and 'the way I've always done it'. But I find it makes for more readable SQL.

Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
2

SQL is a case insensitive programming language, whether u write in CAPITAL or not it will not affect your logic. It is basically done to increase the readability of SQL Query

ph3n0m_aks
  • 35
  • 1
  • 8
2

Entry level SQL:1992 is case sensitive and requires that identifiers (SQL keywords) are in upper case only. Or should I say "required": case insensitive identifiers has been a core standard feature since SQL:1999. I would guess that upper case keywords was the vernacular for programming language design in the late sixties/early seventies.

Nowadays putting SQL keywords in upper case is merely a matter of taste and is my personal preference. I agree that it aids readability and this is why I use lower case for attribute names (with underscore element separators e.g. birth_date) and upper camel case for table names. Not coincidentally, these are the recommended conventions in the only book on SQL heuristics, Joe Celko's SQL Programming Style.

onedaywhen
  • 53,058
  • 12
  • 94
  • 134
1

It's for readability. If we didn't have colored keywords, CAPS would allow us to distinguish keywords.

sam yi
  • 4,689
  • 1
  • 27
  • 39
0

There is no real purpose to it, it is simply a choice of style or convention. It wouldn't affect your them either way, personally I keep all of mine lower case.

shenku
  • 10,902
  • 11
  • 61
  • 113
-5

SQL compiler translates the query written by use to upper case before compiling, It's good to write it in upper case for fast processing

Shiv B
  • 1