How to Format SQL Queries

· 7 min read

Messy SQL is one of the fastest ways to introduce bugs. When a query is a single long line with no indentation, it is hard to see which conditions apply to which joins, where subqueries begin and end, or whether the logic is correct. A browser-based formatter handles the entire job locally without uploading your queries to a server.

Why formatting matters

How to format SQL

  1. Paste your SQL: enter a minified or messy query into the formatter. It handles SELECT, INSERT, UPDATE, DELETE, CREATE TABLE, and complex queries with subqueries and joins.
  2. Configure options: choose indentation size and whether to uppercase keywords. These settings match your project's style guide.
  3. Copy the result: the formatted SQL is ready to paste back into your editor, database client, or documentation.

What good formatting looks like

A query like select u.name, o.total from users u join orders o on u.id = o.user_id where o.total > 100 and u.active = 1 order by o.total desc becomes:

SELECT
  u.name,
  o.total
FROM users u
JOIN orders o
  ON u.id = o.user_id
WHERE o.total > 100
  AND u.active = 1
ORDER BY o.total DESC

Each clause starts on its own line. Conditions are indented under their parent clause. Joins and their ON conditions are clearly paired.

A brief history of SQL formatting conventions

SQL was created by IBM researchers Donald Chamberlin and Raymond Boyce in 1974, originally called SEQUEL (Structured English Query Language). The "QL" in the original name reflected an intent for the language to read like English. From the very beginning, this human-readable design implied a convention: indent your clauses so they read top-to-bottom like sentences.

For most of the 1980s and 1990s, SQL was written by hand in text editors and the formatting was personal. Some shops adopted "river style" (where every keyword aligns vertically on the right of a virtual column), others used "Egyptian style" (curly-brace-on-same-line equivalent), and most just used whatever the author preferred.

The first widely-used SQL formatter was Apex SQL Formatter (2000), followed by Devart's SQL Complete (2002) and Red Gate's SQL Prompt (2003). These tools brought IDE-level formatting to SQL Server and Oracle developers. By 2010 every major IDE (SSMS, DataGrip, DBeaver) had built-in SQL formatting, and online formatters became standard for ad-hoc cleanup.

In 2017 the formatter ecosystem shifted with sql-formatter (npm), an open-source JavaScript library that powers most browser-based SQL formatters today, including this one. Modern formatters handle dialect differences (MySQL backticks, PostgreSQL window functions, SQL Server square brackets) and produce consistent, configurable output.

SQL style guides used by major companies

Most professional codebases follow one of several published SQL style guides:

Style Guide Origin Key conventions
Mozilla SQL Style Mozilla UPPERCASE keywords, snake_case names, 2-space indent
GitLab SQL Style GitLab Data Team UPPERCASE keywords, lowercase names, 4-space indent, leading commas
Holistics SQL Style Holistics UPPERCASE keywords, snake_case, 2 spaces, trailing commas
Simon Holywell SQL Personal/popular "River" alignment, uppercase keywords
dbt SQL Style dbt Labs lowercase keywords (modern dialect), snake_case, leading commas
PostgreSQL Wiki Style PostgreSQL community lowercase keywords, snake_case, K&R-style indentation

If you are starting a new project, pick one of the established guides. If you are joining an existing codebase, follow what is already there. Consistency within a project matters more than any specific style.

Common formatting choices

Dialect differences

SQL formatters need to handle dialect-specific syntax:

Dialect Distinctive features
PostgreSQL Window functions, LATERAL JOINS, dollar-quoted strings ($$), CTE-heavy style
MySQL/MariaDB Backtick identifiers (`table`), LIMIT clause syntax, REPLACE INTO
SQL Server (T-SQL) Square bracket identifiers ([table]), TOP clause, OUTPUT clause, MERGE
Oracle (PL/SQL) DUAL table, ROWNUM, hierarchical CONNECT BY, dot-suffixed package calls
SQLite Limited type system, REPLACE/UPSERT, single-file database
Snowflake Variant data types, QUALIFY clause, COPY INTO
BigQuery Backtick identifiers, ARRAY/STRUCT types, EXCEPT/REPLACE column lists
Redshift PostgreSQL-derived but distinctive DDL, COPY from S3

A good formatter detects or accepts a dialect hint, then handles syntax that other dialects would reject.

Common pitfalls

Tips

Privacy and confidential queries

The SQL formatter runs entirely in your browser. The queries you paste, intermediate processing, and the formatted output all stay on your device. Nothing is uploaded to a server, logged, or shared with anyone.

This matters because SQL queries often contain extremely sensitive information: table names that reveal product architecture, column names that expose business logic and metrics, real customer IDs in WHERE clauses, internal API endpoints in stored procedures, SSNs and credit card numbers in test data, employee compensation in HR queries, financial figures in analytics queries, customer email addresses in marketing queries. Cloud SQL formatters log every query in their request logs, sometimes retain them for "service improvement," and have been involved in real breaches where pasted production queries leaked sensitive schema and data. A browser-based formatter has zero exposure: the query never leaves your machine.

Browser-based formatting also works offline once the page is loaded, useful for formatting queries on airplanes, in secure environments without internet access, or anywhere you cannot or should not paste a database query into a third-party service.

Frequently Asked Questions

Should SQL keywords be uppercase?

It is a widely followed convention to write SQL keywords in uppercase (SELECT, FROM, WHERE) and table or column names in lowercase. This makes queries easier to scan visually. Most style guides recommend it, but it is not required by any database engine.

Does formatting change how the query runs?

No. Whitespace and indentation have no effect on SQL execution. Formatting is purely for human readability. A minified query and a beautifully indented one produce the same result.

What indentation size should I use?

Two or four spaces are both common. Pick whichever your team uses and stay consistent. Most SQL formatters let you configure this.

Is my SQL sent to a server?

No. The formatting happens entirely in your browser. Your queries never leave your device.