SQL Formatter
INPUT
OUTPUT

Formatted SQL appears here

Auto-formats as you type · or press Format

Paste SQL to begin
dialect: sql
🔖
Bookmark this page
Press Ctrl+D to save this tool in your browser for instant access anytime — no sign-up needed.

SQL Formatter & Beautifier Online — Format, Highlight & Minify SQL Queries

Share

About this tool

Free Online SQL Formatter — Beautify, Highlight & Minify SQL Queries Instantly

You have a SQL query that looks like one long unreadable line — everything on a single row, no indentation, mixed keyword casing, no whitespace between clauses. Paste it here and the formatter instantly restructures it: each major clause (SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY) on its own line, columns indented beneath SELECT, AND/OR conditions aligned under WHERE, subqueries indented inside their parentheses. The output has full syntax highlighting — clause keywords in blue, other keywords lighter, strings in green, numbers in red, comments dimmed, quoted identifiers in purple.

The formatter handles the full range of SQL you write day to day. Multi-statement scripts with semicolons are formatted statement by statement with a blank line between each. Subqueries in parentheses are automatically detected and indented one level deeper than the outer query. CTEs (Common Table Expressions) using WITH ... AS (...) format with the CTE body properly indented. CASE/WHEN/THEN/ELSE/END expressions format with each branch on its own line. Window functions with OVER (PARTITION BY ... ORDER BY ...) are handled correctly. Comments — both -- line comments and /* block comments */ — are preserved at the correct indentation level in the formatted output.

Dialect support covers the most common databases: MySQL (backtick identifiers, AUTO_INCREMENT, STRAIGHT_JOIN), PostgreSQL (dollar-quoted strings, :: cast syntax, ILIKE, RETURNING), SQLite (AUTOINCREMENT, GLOB), and SQL Server / T-SQL (square bracket identifiers, TOP, NOLOCK hints, OUTPUT clause). Most standard SQL constructs work identically across all dialects.

The minifier collapses all formatting and removes comments to produce the shortest valid query string — useful for embedding SQL in application code, connection strings, log messages, or any context where compact output matters more than readability.

Query history automatically saves the last 10 formatted queries in your browser, so you can quickly restore a query you formatted earlier without pasting it again. Everything runs 100% client-side — your SQL, table names, column names, and data patterns never leave your browser.

Features

  • Auto-format SQL live as you type — SELECT columns, JOINs, WHERE conditions and subqueries each on their own indented line without any button press
  • SQL minifier — collapse any query to a single compact line with all comments stripped; useful for embedding SQL in JSON config or application code
  • Keyword casing control — convert all keywords to UPPERCASE, lowercase, or preserve original casing; applies to SELECT, FROM, WHERE, JOIN, GROUP BY, and all other reserved words
  • Dialect support — MySQL backtick identifiers, PostgreSQL :: cast and dollar-quoted strings, SQL Server square brackets and TOP, SQLite AUTOINCREMENT all tokenized correctly
  • Full syntax highlighting — clause keywords in blue, other keywords lighter blue, strings green, numbers red, comments dimmed, quoted identifiers purple, operators and punctuation gray
  • Subquery and CTE indentation — parenthesized subqueries indent one level deeper automatically; WITH...AS CTEs format with the body indented inside the parentheses
  • CASE/WHEN/THEN/ELSE/END formatting — each branch on its own indented line, END dedented back to the CASE level; works inside SELECT columns and WHERE conditions
  • Multi-statement SQL scripts — semicolons split the script into independently-formatted statements with blank lines between them; works with mixed SELECT, INSERT, CREATE TABLE scripts
  • File upload and drag & drop — load any .sql or .txt file directly into the editor; download formatted output as query.sql; compare before/after with Diff Checker
  • Query history — last 10 formatted queries auto-saved in your browser; restore any query in one click; 100% private, no SQL ever sent to a server

How to Use

Paste your SQL query into the input panel on the left side. The formatter automatically detects the SQL and applies formatting in real time as you type — each major clause moves to its own line with proper indentation. If you prefer manual control, click the Format button to trigger formatting explicitly. To minify SQL instead of beautifying it, click the Minify button — this removes all whitespace and comments and collapses the query to a single compact line. Use the Keywords dropdown to choose between UPPERCASE keywords (the most common SQL convention), lowercase keywords (popular in some PostgreSQL codebases), or Preserve to leave casing unchanged. Use the Indent dropdown to choose 2 spaces, 4 spaces, or 8 spaces per indentation level — 2 spaces is the default. Select the Dialect dropdown to label your query as MySQL, PostgreSQL, SQLite, or SQL Server — this affects how dialect-specific tokens are recognized. Click Upload or drag a .sql file directly onto the panel to load a file from disk. Click Copy to copy the formatted output to your clipboard, or Download to save it as query.sql. The history panel stores your last 10 queries and lets you restore any of them with a single click.

Common Use Cases

Format a minified SQL query from an ORM or query log
ORMs like Hibernate, ActiveRecord, SQLAlchemy, and Prisma often log queries as single-line strings with no whitespace. Paste the minified log output and the formatter restructures it into readable multi-line SQL with proper clause indentation — making it straightforward to understand what the ORM generated and whether it is doing what you intended. Combine with our JSON Formatter to clean up JSON query parameters alongside the SQL.
Clean up SQL before committing to version control
SQL stored in migration files, seed scripts, and stored procedures should be consistently formatted before committing to git — otherwise every developer's editor produces different whitespace and diffs become noise. Paste the SQL here, format it with your team's chosen convention (2-space indent, UPPERCASE keywords), copy, and paste back. Use our Diff Checker to verify exactly what changed between the old and new version before committing.
Debug a complex query with subqueries and multiple JOINs
Multi-table queries with nested subqueries, multiple JOINs, and long WHERE conditions are difficult to read as flat text. Formatting them makes the structure immediately visible — each JOIN is on its own line, each subquery is indented inside its parentheses, and AND/OR conditions are aligned under the WHERE clause. This makes it easier to spot missing JOIN conditions, incorrect column references, and logic errors in complex analytical queries.
Standardize keyword casing across a legacy codebase
Legacy SQL codebases often have inconsistent keyword casing — some files use SELECT, others use select or Select. Paste each query and use the UPPERCASE keywords option to normalize the casing, then copy back. The formatter never changes identifiers, string values, or comments — only SQL reserved keywords are affected, making it safe to run on production queries without risk of semantic changes.
Embed SQL in application code after minifying
SQL strings embedded in Python, JavaScript, Go, or Java application code are easier to maintain as minified single-line strings in some codebases. Use the Minify button to collapse your formatted query to a compact single line with all comments stripped — ready to paste into a string literal or configuration file. The minifier preserves all query semantics and handles string literals and quoted identifiers correctly.
Prepare SQL for documentation, reports, or code review
SQL shared in documentation, pull request descriptions, Confluence pages, Slack messages, or technical reports should be formatted and consistently cased. Paste your query, format it with UPPERCASE keywords and 2-space indentation, then download as query.sql or copy to clipboard. The syntax-highlighted output in the right panel can also be screenshot for documentation using our Code Screenshot Generator.

Frequently Asked Questions

Paste your SQL query into the input panel on the left. The formatter automatically applies proper indentation and formatting as you type — no button press required. If you prefer to trigger formatting manually, click the Format button in the toolbar. The formatted output appears in the right panel with syntax highlighting. You can then click Copy to copy it to your clipboard or Download to save it as a .sql file.

The formatter handles Generic SQL, MySQL, PostgreSQL, SQLite, and SQL Server (T-SQL) syntax. The dialect selector in the toolbar lets you label your query with the target database. The tokenizer handles dialect-specific syntax including MySQL backtick identifiers, PostgreSQL dollar-quoted strings and :: cast syntax, SQL Server square bracket identifiers and TOP clause, and SQLite-specific functions. Most standard SQL constructs — SELECT, JOIN, subqueries, CASE/WHEN, window functions, CTEs — work across all dialects.

Formatting (beautifying) adds proper indentation, newlines before each major clause, and consistent keyword casing to make queries readable for humans. Each SELECT column, WHERE condition, and JOIN clause is placed on its own indented line. Minifying does the opposite — it removes all unnecessary whitespace and comments to produce the shortest possible query string. Minified SQL is useful for embedding queries in code strings, logging, or sending over a wire where readability is less important than compactness.

Use the Keywords dropdown in the toolbar. Select UPPERCASE to convert all SQL keywords (SELECT, FROM, WHERE, JOIN, etc.) to uppercase — the most common convention in professional SQL. Select lowercase to convert all keywords to lowercase — common in some PostgreSQL and application-layer codebases. Select Preserve to leave keyword casing exactly as typed, without modification. The setting applies instantly without needing to reformat.

Yes. The formatter handles multiple SQL statements separated by semicolons. Each statement is formatted independently, with a blank line between them in the output for readability. This works for scripts containing a mix of SELECT, INSERT, UPDATE, DELETE, CREATE TABLE, ALTER TABLE, and other statement types. Comments (-- line comments and /* block comments */) are preserved and formatted at the correct indentation level.

Yes. Subqueries inside parentheses are detected automatically and indented one level deeper than the outer query. Common Table Expressions (CTEs) using WITH ... AS (...) are formatted with the CTE body properly indented. Nested subqueries increase indentation with each level. CASE/WHEN/THEN/ELSE/END expressions are formatted with each WHEN and ELSE on its own indented line, and END is dedented back to the CASE level.

Yes — all SQL formatting runs entirely in your browser using JavaScript. Your queries are never sent to any server, never logged, and never stored outside your browser tab. This makes the tool safe for formatting queries that contain sensitive table names, column names, production data patterns, credentials in connection strings, or any proprietary database schema. Closing the tab permanently discards everything you pasted.

Yes. Click the Upload button in the toolbar to select a .sql or .txt file from your computer, or drag and drop the file directly onto the panel. The file contents are loaded into the input editor and formatted automatically. You can then download the formatted output using the Download button, which saves the result as query.sql.