Explain Codes LogoExplain Codes Logo

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

sql
best-practices
sql-style
readability
Alex KataevbyAlex Kataev·Sep 21, 2024
TLDR

UPPER CASE for SQL keywords boosts readability by providing contrast with variable elements like table or column names. For example:

-- Let's SELECT, not infect! SELECT * FROM Customers WHERE Age > 30;

Following this convention throughout your SQL scripts promotes clarity and consistency, assisting in code navigation and maintenance. And since SQL is case-insensitive, functionality isn't affected.

Yet, your personal preference also matters. A lower case approach might keep you focused on content rather than syntax, and mixed case names can offer additional visual cues.

History and style choice

Long ago, limited environments popularized the uppercase convention for SQL keywords. Nowadays, modern IDEs with syntax highlighting render this less of a technical necessity.

Using lowercase can make your SQL code look streamlined and fluid, keeping the focus on business logic rather than syntax. This approach can also help literals and comments stand out.

Weighing readability and comfort

Importance of consistent style

Whether you go for an upper or lower case, consistency is critical. A common style guide helps to read, understand, and maintain code efficiently, especially in a team or open source environment.

Better typing experience

If you frequently write SQL, opting for a case that avoids usage of the shift key can lead to a less strainful typing experience, potentially increasing your productivity and comfort.

Object names: an argument for MixedCase

Consider using MixedCase for object names as it distinguishes them from both SQL keywords and the rest of the query:

-- It's MixedCase, not mixed up! SELECT MixedColumn FROM MixedTable WHERE SomeColumn IS NOT NULL;

Debunking the upper-case performance myth

You might have heard that using upper case boosts query performance. Let's bust this myth: SQL parsers treat upper and lower case equally, generating the same optimized query plan.

Just go with the flow

With coding, adaptability reflects strength. In one project, you might need to conform to specific conventions, and in another, you might choose your own. Be willing to adjust your SQL writing style as needed.