Why uppercase SQL is so common, and why it doesn't make sense
SQL, or Structured Query Language, is a widely used language for interacting with databases. SQL allows you to create, manipulate, and query data in a structured and efficient way.
HOWEVER, THERE IS ONE ASPECT OF SQL THAT OFTEN SPARKS DEBATE AMONG DEVELOPERS - WHICH CASE DO YOU USE
..AND WHY DOES IT FEEL LIKE I'M BEING YELLED AT!?Agenda
A few months ago I joined a project where a codebase was a mix of uppercase and lowercase SQL. I created a migration file and one comment during code review was "It would be better if this was uppercase, as it's good practice to do so". That gave me some mixed feelings - it's not like they followed any standard either.
Most of my previous teams and projects used lowercase SQL. Back when I studied at university or looked at SQL code samples online, a lot of times it's presented in uppercase. I started to wonder why. Is it actually "good practice"? Is it because we were just taught that way, or are there other reasons?
So I went on a research rabbithole. It all comes down to history.
How uppercase SQL came to be
If you look at most SQL tutorials, books, or documentation, you will notice that the SQL keywords are usually written in uppercase.
This convention has a historical reason: back in the days when SQL was first developed, most computer systems used monospaced fonts and no syntax highlighting. This made it hard to distinguish between SQL keywords and other identifiers, such as table names, column names, or variables. To solve this problem, developers adopted the practice of writing SQL keywords in uppercase, so that they would stand out from the rest of the code.
Another major reason for using uppercase SQL keywords is that.. SQL is old.
SQL dates back to 1974. At that time, many keyboards only had uppercase letters, so the language documentation used them as well. This was a common practice in the early days of computing.
Stick to lowercase
However, times have changed, and so have the tools that we use to write and execute SQL. Nowadays, most modern editors and IDEs support proportional fonts and syntax highlighting, which make it much easier to read and write SQL code.
In fact, using uppercase SQL keywords can have some drawbacks, such as:
- It is harder to read: Studies have shown that lowercase text is easier to read than uppercase text, because lowercase letters have more distinctive shapes and patterns, while uppercase letters are more uniform and similar. This means that using uppercase SQL keywords can make your code less readable and more prone to errors.
- It is harder to type: Typing uppercase letters requires more keystrokes and more effort than typing lowercase letters, especially if you use a keyboard layout that requires you to press the Shift key or the Caps Lock key to switch between cases. This can slow down your typing speed and increase your fatigue. Rest that pinky!
- It is harder to maintain: If you use uppercase SQL keywords, you have to be consistent and use them throughout your code, otherwise your code will look messy and unprofessional. However, this can be tedious and error-prone, especially if you have to edit or refactor your code later. You may forget to change the case of some keywords, or introduce typos or inconsistencies. However, keep in mind that this goes both ways. It is, however, just easier to lowercase everything - let your editor handle the indentation and syntax highlightning for you.
- There’s no difference from a technical point of view. Your queries are the same speed in either case.
Many developers on Stackoverflow also do not endorse the use of uppercase syntax, and bring out some pretty great points.
Of course - if your codebase already has a set standard and actually enforces uppercase, then please do use it. I'm giving my pinky a break though.
The Bottom Line
In conclusion, the case of your SQL keywords doesn't really matter that much, because SQL is a case-insensitive language, meaning that it treats uppercase and lowercase letters as equivalent.
However, using lowercase SQL keywords can make your code more readable, more writable, and more maintainable, and improve your overall coding experience.
Therefore, I recommend that you use lowercase SQL keywords in your SQL code, and enjoy the benefits of this simple but effective practice.