HomeFormattersCodeSQL Formatter

SQL Formatter

Code

Format and beautify SQL queries with proper indentation and keyword casing. Runs entirely in your browser — your queries are never uploaded or stored anywhere.

Reviewed by the thecalcu.com team · Last updated June 20, 2026

What is a SQL?

The SQL Formatter restructures SQL queries by placing each major clause on its own line, adding consistent indentation under AND/OR conditions, and standardising keyword casing, transforming compact single-line queries into readable, structured statements that are easy to scan, review, and debug.

SQL is frequently written and transmitted as a single dense line. ORMs generate it that way, log files capture it that way, and developers under time pressure write it that way. The result is queries like:

select u.id, u.name, o.total from users u inner join orders o on u.id = o.user_id where o.total > 1000 and u.active = 1 order by o.total desc limit 10

That query has seven logical clauses, SELECT, FROM, INNER JOIN, ON, WHERE, ORDER BY, LIMIT, but none of them stand out when they are strung together on one line. The SQL Formatter turns this into a statement where each clause opens on its own line, AND conditions are indented to show they belong to the WHERE clause, and keywords are uppercase to distinguish SQL syntax from schema identifiers.

The formatter is dialect-agnostic and works with MySQL, PostgreSQL, SQLite, SQL Server, and Oracle SQL. It applies rule-based formatting rather than full grammar parsing, which means it handles the common clauses perfectly and does not crash on unfamiliar syntax. All formatting runs entirely in your browser, your queries are never sent to a server or stored anywhere.

Use the JSON Minifier for compacting JSON API payloads that accompany your SQL-backed services, or the JSON Formatter to inspect and debug JSON responses alongside formatted queries.

Why Use a SQL Formatter?

SQL that arrives as a single line takes significantly longer to read than SQL laid out with one clause per line. When reviewing a colleague's pull request that touches a complex query, or debugging a query that returns wrong results, you cannot hold the logic of a 200-character single-line statement in your head all at once. Formatted SQL lets you read the WHERE clause independently of the JOIN, verify the GROUP BY columns against the SELECT list, and trace the order of operations.

Formatted SQL is also essential for version control. An ORM migration that changes one WHERE condition in a multi-clause query will produce a one-line diff if the SQL is compact, making it impossible to see what changed without careful character-by-character reading. After formatting, the change shows as a single modified line in the WHERE section, immediately visible.

Standardised keyword casing removes ambiguity about what is a SQL reserved word and what is a user-defined identifier. When keywords and table names share similar names (e.g. a status column versus the keyword SET), uppercase keywords make the distinction obvious.

Who Should Use This Formatter?

Database developers and DBAs reviewing and optimising queries will find the formatter essential for reading ORM-generated SQL. Most ORMs produce single-line queries with mixed-case keywords that are difficult to audit. Paste the generated SQL into the formatter to see its structure before analysing the execution plan.

Back-end developers debugging slow queries or incorrect results will find that formatted SQL reveals structural issues that are invisible in compressed form, for example, a condition placed in the HAVING clause instead of WHERE, or an ON clause that references the wrong table alias.

Data analysts writing ad-hoc reports in BI tools often build queries iteratively, copying fragments between tools. The formatter provides a clean, readable version for documentation and sharing with colleagues who need to understand the query's logic.

Students learning SQL benefit from seeing well-structured examples where clause boundaries are unambiguous. Formatted SQL is also what most SQL textbooks and tutorials show, so this tool helps bridge the gap between a working query and the style used in learning materials.

What Insights Does the SQL Formatter Give You?

Formatted SQL makes the following structural elements immediately visible:

  • Which columns are in the SELECT list, each column reads clearly instead of being buried in a comma-separated run
  • How many tables are joined and on what conditions, JOIN/ON pairs stand out as distinct blocks
  • How many conditions are in the WHERE clause, AND/OR connectors are indented under WHERE, making the filter logic readable at a glance
  • Whether GROUP BY and HAVING are used, placed on their own lines, these are easy to spot in a long query
  • The sort order and row limit, ORDER BY and LIMIT are clearly separated from the data selection logic

How to use this SQL calculator

  1. Paste your SQL query into the SQL Query input box, this can be a single query, a subquery, or a multi-statement script.
  2. Choose an Indent Size: 2 spaces for compact output, 4 spaces (default) for the most readable layout.
  3. Toggle Uppercase keywords on (default) to capitalise SQL reserved words, or off to keep them lowercase.
  4. The Formatted SQL output updates instantly as you type or change options.
  5. Click Copy to copy the formatted query to your clipboard.

Formula & Methodology

The formatter uses a single-pass regex replacement to insert newlines before major clause keywords. Multi-word keywords (GROUP BY, ORDER BY, LEFT JOIN, INNER JOIN, etc.) are matched before their single-word components to ensure the longer form is captured intact.

Keywords that trigger a new line: SELECT, FROM, WHERE, JOIN (and all JOIN variants), ON, GROUP BY, ORDER BY, HAVING, LIMIT, OFFSET, UNION, UNION ALL, INTERSECT, EXCEPT, INSERT INTO, UPDATE, SET, VALUES, DELETE FROM, CREATE TABLE, ALTER TABLE, DROP TABLE.

AND and OR are indented by the chosen indent size under the current clause, rather than starting a new top-level line.

All whitespace is normalised (collapsed to single spaces) before formatting, so the output is consistent regardless of the original spacing.

Before:
sql select u.id, u.name, o.total from users u inner join orders o on u.id = o.user_id where o.total > 1000 and u.active = 1 order by o.total desc limit 10 

After (4 spaces, uppercase keywords):
sql SELECT u.id, u.name, o.total FROM users u INNER JOIN orders o ON u.id = o.user_id WHERE o.total > 1000     AND u.active = 1 ORDER BY o.total desc LIMIT 10 

Frequently Asked Questions

SQL formatting (also called SQL beautification or pretty-printing) restructures a SQL query by placing each major clause on its own line, adding consistent indentation, and standardising keyword casing. A single-line query like `select u.id from users u where u.active=1 order by u.id` becomes a structured multi-line statement where every clause is immediately visible. Formatted SQL is easier to read, review, and debug than compact single-line SQL.
Unformatted SQL is difficult to read and nearly impossible to review in a code diff. When a query spans a WHERE clause with ten conditions, a multi-table JOIN, and a GROUP BY, all run together on one line, identifying which condition belongs to which clause is genuinely hard. Consistent formatting makes the logical structure visible at a glance, reduces the time spent debugging incorrect results, and makes code review and version control diffs significantly cleaner.
SQL is case-insensitive for keywords, so both `SELECT` and `select` are valid. However, most style guides recommend uppercase keywords to visually distinguish reserved words from identifiers (table names, column names). Uppercase keywords make it immediately clear what is SQL syntax and what is your schema. This formatter offers an 'Uppercase keywords' option that is enabled by default.
The SQL Formatter is dialect-agnostic, it formats based on standard SQL keywords that are common to MySQL, PostgreSQL, SQLite, SQL Server (T-SQL), and Oracle SQL. It recognises SELECT, FROM, WHERE, JOIN variants, GROUP BY, ORDER BY, HAVING, LIMIT, OFFSET, INSERT INTO, UPDATE, SET, DELETE FROM, and DDL statements like CREATE TABLE and ALTER TABLE. Dialect-specific syntax (stored procedures, window function syntax, vendor extensions) may not be formatted perfectly but will not be corrupted.
Paste your SQL query into the SQL Query input box, choose your preferred indent size (2 or 4 spaces), and toggle 'Uppercase keywords' if you want keywords in all caps. The formatted output appears instantly. Click the Copy button to copy the formatted SQL to your clipboard.
Yes. The formatter processes the entire input as a single block of text, placing major clause keywords on new lines throughout. Multi-statement scripts with multiple SELECT, INSERT, or UPDATE statements will each have their clauses formatted. Statement separators (semicolons) are not specially handled, they remain at the end of the last clause they follow.
No. This is a rule-based formatter that rearranges whitespace and line breaks based on keyword positions. It does not parse the full SQL grammar and cannot detect or fix syntax errors, missing parentheses, or incorrect clause ordering. To identify syntax errors, run the query against your database or use a dialect-specific SQL editor with error highlighting.
Nothing is transmitted. All formatting happens entirely in your browser using JavaScript. The SQL you paste is never sent to any server, stored, or logged. You can safely format queries that contain internal table names, schema details, or sensitive filter conditions.
Yes, once the page has loaded, the formatter runs without a network connection. The formatting logic is pure JavaScript that executes locally in your browser tab, with no external dependencies.
Four spaces per level is the most common SQL convention and is the default. Two spaces produces more compact output that is useful when the query has many nested subqueries or when you need to fit the formatted SQL into a narrow column in documentation. Neither choice affects how the database executes the query.
The formatter handles inline subqueries and nested SELECT statements, it will format the keywords inside the subquery just as it does at the top level. Subqueries wrapped in parentheses are processed as part of the overall token stream, so their clauses are also placed on new lines. Very deeply nested subqueries (more than two levels) may produce output that looks cleaner with 2-space indentation.
Also known as
SQL beautifierformat SQL querySQL pretty printerSQL indenterSQL code formatterquery formatter