Formatted SQL appears here
Auto-formats as you type · or press Format
SQL Formatter & Beautifier Online — Format, Highlight & Minify SQL Queries
What's included
Features
:: cast and dollar-quoted strings, SQL Server square brackets and TOP, SQLite AUTOINCREMENT all tokenized correctlyAbout this tool
Online SQL Formatter — Free, 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.
Step by step
How to Use
- 1Paste your SQL into the input panelPaste any SQL query into the left input panel. The formatter auto-formats in real time as you type — each major clause (SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY) moves to its own line with proper indentation. Click Sample to load a realistic multi-table e-commerce query with JOINs, a CASE expression, GROUP BY, HAVING, and ORDER BY. Click Paste to paste clipboard content directly. Drag and drop a
.sqlfile or click Upload to load from disk. - 2Set keyword casing and indent sizeUse the Keywords dropdown to choose: UPPERCASE (the most widely-used SQL convention), lowercase (common in some PostgreSQL codebases), or Preserve (leave casing unchanged). Use the Indent dropdown to choose 2, 4, or 8 spaces per indentation level. Both settings apply live — changing them updates the output without re-clicking Format.
- 3Select a dialectUse the Dialect dropdown to label your query as Generic SQL, MySQL, PostgreSQL, SQLite, or SQL Server. The tokenizer handles dialect-specific syntax: MySQL backtick identifiers, PostgreSQL
::cast and dollar-quoted strings, SQL Server square bracket identifiers and TOP, SQLite AUTOINCREMENT. Most standard constructs work across all dialects. - 4Format manually or minifyClick Format to trigger explicit formatting (useful if you turned off auto-format). Click Minify to collapse the query to the shortest possible single-line string with all comments stripped — useful for embedding SQL in application code, log entries, or config files where compactness matters more than readability.
- 5Copy the formatted output or download itClick Copy in the toolbar to copy the full formatted SQL to your clipboard. Click Download to save the output as
query.sql. The stats bar at the bottom shows the number of statements, total keyword count, and line count for the formatted output. - 6Use history to restore previous queriesThe tool auto-saves the last 10 formatted queries to your browser history. Click History in the toolbar to open the history panel. Click any entry to restore that query instantly. This is useful when you formatted a query, navigated away, and want to recover the work without pasting again.
Real-world uses
Common Use Cases
Got questions?
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.