Skip to content

SQL formatter

Paste a query to lay it out clearly.

0 characters · 0 lines

0 characters · 0 lines

Your files are open only in this tab. Nothing is uploaded.

Works with PostgreSQL, MySQL, SQLite and ANSI SQL. It changes only spacing and keyword case, never the query logic itself.

How to use SQL formatter

  1. Add your SQL input. Paste dense SQL queries from ORM logs and traces into the SQL field, click Sample for a demo join query, or drop a .sql file.
  2. Pick formatting options. Choose Format or One line under Mode, then set Indent to 2 spaces, 4 spaces, or Tabs and toggle Uppercase keywords.
  3. Copy or download the result. Click Copy to place the formatted output on your clipboard or Download to save query.sql locally.

Everything runs in this browser tab. Nothing you paste or open is uploaded or logged, so it's safe for keys, tokens and private data.

Pretty print SQL queries for PostgreSQL, MySQL, and SQLite

Long single-line queries pulled from application logs or ORM debuggers hide join conditions and nested filters. Using a private SQL beautifier online turns tangled strings into scannable, clause-per-line blocks you can review in a pull request or run safely in a database console.

Indented subqueries, CTEs, and multi-line JOINs

In Format mode, the lexer scans up to 3 words ahead to align multi-word clauses such as SELECT DISTINCT, LEFT JOIN, GROUP BY, HAVING, ORDER BY, ON CONFLICT, and WITH RECURSIVE on fresh lines. Column lists after SELECT, GROUP BY, ORDER BY, SET, and VALUES break after every comma so each field sits at a consistent indent level.

Parentheses containing nested SELECT or WITH subqueries increase indentation automatically and align the closing parenthesis back to the outer block, following standard clause structure in the SQLite SELECT Syntax Documentation. Function calls such as count(o.id) or coalesce(a, b) stay compact on one line without extra inner line breaks. After running a query and exporting tabular rows, you can convert query result CSV exports into JSON for downstream API testing.

Choosing 2 spaces, 4 spaces, or Tabs with keyword casing

Under Mode, Format unlocks two layout controls: Indent (2 spaces, 4 spaces, or Tabs) and Uppercase keywords. Leaving Uppercase keywords checked converts reserved words like select, from, and count to uppercase (SELECT, FROM, COUNT) while keeping table aliases and column names untouched.

Unchecking Uppercase keywords normalizes reserved words to lowercase if your team style guide prefers lowercase SQL. Changing any option updates Formatted output immediately without re-uploading or reloading the page.

How does a SQL formatter compare to a linter when parsing casts and strings?

Unlike a database linter that validates schema tables or flags slow joins, a formatter focuses purely on layout and lexical safety. Naive find-and-replace formatters corrupt SQL by uppercasing words inside quotes or breaking table.column references at reserved keywords, whereas token-aware lexing classifies every string, comment, operator, and identifier first.

Preserving :: casts, table.column names, and BETWEEN clauses

When you format a PostgreSQL query, lexical rules in the PostgreSQL SQL Lexical Structure Documentation distinguish keywords from single-quoted string constants, double-quoted identifiers, and :: cast operators. The tokenizer keeps '2024-01-01', "select", and payload::jsonb intact with zero extra spaces around dots or double colons.

Inside WHERE and HAVING clauses, AND and OR start new indented lines unless they appear inside a function call or a BETWEEN x AND y range expression. If you are preparing database schema migrations across branches, compare two formatted SQL migration scripts side by side to spot every altered column or index.

Handling multiple semicolon-separated statements and comments

You can paste a full migration script containing multiple queries terminated by ;. The formatter separates each statement with 1 blank line, resets indentation depth at every semicolon, and keeps -- single-line comments and /* ... */ block comments in place.

Client-side privacy and Web Worker speed for large .sql scripts

Database queries often contain internal table schemas, customer emails in WHERE filters, or proprietary reporting logic that should never leave your workstation. Running 100% of tokenization and formatting locally in JavaScript keeps sensitive SQL completely private on your device.

Keeping schema names and production filters off remote servers

Unlike hosted SQL beautifiers that POST your query to a backend API, this tool processes every character inside your browser tab. You can disconnect from the network once the page loads and continue formatting .sql files with zero external requests.

When you need to inspect JSON columns extracted from PostgreSQL jsonb queries, validate and prettify exported JSON payloads locally in the same privacy-first workflow.

Non-blocking worker formatting for multi-statement files

Large migration dumps and generated ORM scripts are routed to a background thread built on the MDN Web Workers API Reference. Offloading tokenization keeps typing and dropdown controls responsive whether you paste a 20 KB query or a 1 MB .sql migration file.

When should you format SQL vs minify it to one line?

Use Format mode during code reviews, query optimization, and documentation so every join, filter, and subquery is clear. Switch to One line mode when pasting queries into environment variables, JSON configs, shell commands, or single-line log filters where newlines and comments cause issues.

Both modes handle standard DDL and DML statements aligned with ISO/IEC 9075 SQL standards and the MySQL 8.4 SQL Statement Syntax Reference. Running 100% locally in a background Web Worker ensures even 50 KB migration files format cleanly without stalling your browser. Compare both modes and option behaviors in the table below.

Mode or option Indent & casing Comments (--, /* */) Line breaks Best use case
Format (2 spaces) 2-space indent, UPPER keywords Preserved One clause per line Pull requests, docs, IDE editing
Format (4 spaces) 4-space indent, UPPER keywords Preserved One clause per line Python/SQL style guides, wide screens
Format (Tabs) Tab indent, lower/UPPER Preserved One clause per line Tab-indented repositories
One line Single space, original case Stripped completely Removed (1 line) Shell args, logs, compact configs

Tip: Before embedding a query inside a YAML deployment manifest or config file, switch to One line to strip comments and newlines, or convert structured config payloads with JSON to YAML.

Questions

Can formatting change what my SQL query does?

No. In Format mode, the tool only adjusts whitespace, line breaks, and unquoted keyword casing. String literals, quoted identifiers, numeric constants, and comments stay intact so query execution never changes.

Which SQL dialects does this formatter support?

It works cleanly with PostgreSQL, MySQL, SQLite, and standard ANSI SQL. It recognizes common clauses like SELECT DISTINCT, LEFT JOIN, GROUP BY, HAVING, WITH RECURSIVE, ON CONFLICT, and RETURNING, plus PostgreSQL :: casts.

Does One line mode remove SQL comments?

Yes. Switching Mode to One line runs a minifier that strips both -- single-line comments and /* ... */ block comments while collapsing extra spaces into a single compact line.

Why does BETWEEN ... AND stay on a single line?

In WHERE and HAVING clauses, standalone AND and OR operators break onto new indented lines for readability. The formatter detects BETWEEN <value> AND so range predicates stay together on one line.

How does the formatter handle table.column names that match keywords?

Whenever a word immediately follows a dot (such as u.order or item.select), the parser treats it as a member identifier rather than a SQL keyword, keeping its original casing and avoiding unwanted line breaks.

Does this tool check whether a query is valid for my database?

No. This formatter acts as a layout beautifier rather than a database linter or compiler. It arranges tokens, indentation, and keyword case without checking whether table names, column types, or dialect-specific functions exist in your database.

Can I upload a .sql file and download the formatted result?

Yes. Drop or open any .sql or plain-text file in the SQL input area, adjust Indent and Uppercase keywords, and click Download to save the output as query.sql.

Updated