Should You Write SQL Code in Uppercase or Lowercase?

Lowercase SQL keywords are better for readability and modern coding standards. This article explores differences between uppercase and lowercase SQL code.
On this page

Should You Write SQL Code in Uppercase or Lowercase?

Excerpt

Lowercase SQL keywords are increasingly standard given their readability and adherence to modern coding conventions.


The choice between uppercase and lowercase for SQL keywords is a common style question that developers face. While SQL itself is case agnostic, conventions and readability may inform this SQL case decision. In this comprehensive guide, we’ll compare uppercase and lowercase styles in detail across a variety of factors.

SQL Keywords Are Not Case Sensitive

To start, it’s important to note that SQL keywords are not case sensitive - SELECT functions the same as select when parsed by the database engine. The SQL ANSI standard accepts both uppercase and lowercase syntax. Certain databases like Oracle recommend uppercase for standards alignment, while MySQL is fully case insensitive. But in general, developers have discretion to choose case style.

This gives flexibility, but also requires thought on conventions and readability tradeoffs.

Historical Precedent of Uppercase SQL

Uppercase SQL keywords were prevalent in early SQL standards and commercial databases starting in the 1970s and 1980s. For example:

1SELECT * FROM CUSTOMERS;

At the time, UPPERCASE keywords stood out clearly from surrounding text, making queries more readable. This improved scannability and became the standard coding practice.

Using uppercase also differentiated SQL from other case-sensitive programming languages. Overall, uppercase style matched the historical origins of SQL in academia and large enterprises.

Transition Towards Lowercase Style

As development and style conventions evolved through the 1990s and 2000s, lowercase syntax became increasingly common across languages like Java, Python, JavaScript and more.

In parallel, many adopted lowercase styling for SQL:

1select * from customers;

Some drivers of this transition:

  • Readability - Lowercase can improve readability for long, complex queries
  • Consistency - Adopting conventions of other coding languages
  • Style Guides - Newer guides recommended lowercase for modern SQL

You can quickly convert text to lowercase online to match this style.

Comparing Readability of Cases

Readability is a major factor in choosing SQL case. Some key considerations around readability:

  • Lowercase for Long Queries - In long queries with many keywords, lowercase may be less visually distracting and easier to parse quickly.

  • Uppercase Highlights Keywords - Uppercase makes keywords like SELECT and FROM stand out more, improving scannability.

  • Formatting Aids Readability - Adding line breaks, spacing and indentation can aid readability of both cases.

You can compare uppercase and lowercase conversions online to see the readability difference. Often a mix of uppercase keywords and lowercase identifiers offers a balanced style.

Consistency Matters

Beyond readability, consistency of formatting also impacts SQL usability. Mixing cases inconsistently often harms readability even if each case style alone is readable.

Many modern style guides promote lowercase SQL for consistency with other coding languages. But traditional uppercase still has its proponents.

Whichever case you choose, using it consistently improves comprehension. Code should optimize for overall consistency, not individual developer habit.

Match Identifier and Keyword Cases

In SQL code, identifiers like table and column names are typically lowercase or snake_case, for example customer or order_id.

Using the same lowercase case for both keywords and identifiers improves overall consistency and scans well visually. This again tips towards lowercase keywords for most SQL use cases.

Formatting Enhances Uppercase Readability

For developers who prefer uppercase SQL, proper formatting can aid readability:

  • Insert spaces between spaced keyword pairs like SELECT *
  • Add line breaks to separate query clauses
  • Use indentation to denote code structure

This improves visual parsing while keeping keywords uppercase. However, uppercase with formatting is still less common than lowercase style.

Personal Preference Still Plays a Role

Beyond the above technical factors, personal preference still influences developers’ SQL case choices:

  • Familiarity - Those used to uppercase often stick with habit
  • Readability - Many find lowercase subjectively more readable
  • Style - Some view lowercase as more modern and preferred

When guidelines clash, experience and intuition may dictate individual case style.

Key Takeaways on SQL Case

Given the various considerations around SQL case, here are some key conclusions:

  • SQL itself allows either case, providing flexibility
  • Uppercase has historical precedent while lowercase is widely used
  • Matching case between keywords and identifiers improves consistency
  • Readability depends on query complexity and formatting
  • Consistent case is critical, not just individual words
  • Formatting tweaks can optimize readability for either case
  • Personal coding style often affects case preferences

Recommendations for SQL Case Usage

Based on the above analysis, here are recommendations on Selecting SQL case:

  • Pick one case style for consistency within a codebase
  • Lean towards lowercase as a modern convention
  • Use formatting like spacing to augment readability
  • Follow any database or framework case conventions
  • Standardize on case for readability and maintainability
  • Use online converters to evaluate alternate cases

While there is no single right answer, these tips can guide your SQL case decision. Be thoughtful in your choice, not just habitual, for long-term readability.