Is There a Good Reason to Use Upper Case for SQL Keywords

Is there a good reason to use upper case for SQL keywords?

It's just a matter of style, probably originating in the days when editors didn't do code colouring.

I used to prefer all upper case, but I'm now leaning towards all lower.

Either way, be consistent.

Why should I capitalize my SQL keywords? Is there a good reason?

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

Lowercase or uppercase for SQL Statements

You have two separate issues:

  1. Whether you use uppercase or lowercase for SQL keywords. This is a matter of taste. Documentation usually uses uppercase, but I suggest you just agree on either in the particular project.

  2. How to quote SQL in your host language. Since strings are quoted with single quotes (') in SQL, I suggest you use double quotes (") to quote SQL statements from your host language (or tripple quotes (""") if your host language is python). SQL does use double quotes, but only for names of tables, columns and functions that contain special characters, which you can usually avoid needing.

    In C/C++ there is a third option for quoting. If your compiler supports variadic macros (introduced in C99), you can define:

    #define SQL(...) #__VA_ARGS__

    and use SQL like SQL(SELECT * FROM users WHERE id = 1). The advantage is that this way the string can span multiple lines, while quoted strings must have each line quoted separately. The tripple quotes in python serve the same purpose.

Why are the queries in SQL mostly written in Capital Letters?

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.

Why are SQL entries written in uppercase?

SQL was developed in the 1970s when the popular programming languages (like COBOL) used ALL CAPS, and the convention must have stuck.

Why are MySQL queries almost always written in Capital

It's just a matter of readability. Keeping keywords in upper case and table/column names lower case means it's easier to separate the two when scan reading the statement .: better readability.

Most SQL implementations are case-insensitive, so you could write your statement in late 90s LeEt CoDeR StYLe, if you felt so inclined, and it would still work.

What is POPULAR today when it comes to write SQL Queries: UPPER CASE or Lower Case?

Either. Just be consistent

Personally...

Keywords uppercase for me, objects and datatypes in Normal or CapsCase.

SELECT 
M.Column1,
CAST(O.Column2 AS int) AS IntColumn
FROM
MyTable M
JOIN
OtherTable O ON M.SomeKey = O.SomeKey
WHERE
M.Column3 = 'bar'
ORDER BY
M.Column1

It's easier to pick out the clauses of a complete SELECT query this way

Is SQL syntax case sensitive?

The SQL keywords are case insensitive (SELECT, FROM, WHERE, etc), but they are often written in all caps. However, in some setups, table and column names are case sensitive.

MySQL has a configuration option to enable/disable it. Usually case sensitive table and column names are the default on Linux MySQL and case insensitive used to be the default on Windows, but now the installer asked about this during setup. For SQL Server it is a function of the database's collation setting.

Here is the MySQL page about name case-sensitivity

Here is the article in MSDN about collations for SQL Server

Why use capital letters for SQLite column names?

SQL is case insensitive. You can use lower or uppercase characters. There seems to exist a general consensus for using capital letters for SQL keywords, camel case for tables, and lower case for fields, but that is not written in stone either.

If you do not have to follow imposed formatting guidelines, you can use whichever notation you prefer. Stick to that throughout your project, as that is a good habit to have.



Related Topics



Leave a reply



Submit