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.

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.

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
What is SQL formatting?
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.
Why does SQL formatting matter?
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.
Should SQL keywords be uppercase or lowercase?
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.
What SQL dialects does this formatter support?
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.
How do I format a SQL query?
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.
Does this formatter work on multi-statement SQL scripts?
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.
Can this fix syntax errors in my SQL?
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.
Is my SQL uploaded anywhere?
No. 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.
Does this work offline?
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.
What indent size should I use?
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.
Can I use this for subqueries?
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.