SQL Formatter - Beautify and Minify SQL Queries
Beautify or minify SQL queries across dialects, indent clauses and subqueries, set keyword case, and strip comments.
Need to turn a column of IDs or values into a SQL IN (...) clause? Switch to our Delimited List Converter.
🔒 Data Safety: We process everything securely and never store or share it.
How to Use
- Enter or paste your SQL query above, or open or drop a local .sql file.
- Select your SQL dialect, indentation style, and keyword case, and check "Strip Comments" if needed.
- Press "Format" to structure your clauses, apply keyword casing, or minify the query into one line.
- Copy the formatted SQL to your clipboard, or press "Download" to save a .sql file.
Options Explained
- SQL Dialect: Keep "Standard SQL (Auto)", or pick "MySQL / MariaDB" for backticks, # comments, and backslash escapes. Select "PostgreSQL" to parse $$ dollar quotes, nested /*...*/ block comments, and :: type casts. Choose "SQL Server (T-SQL)" for [brackets], GO batch separators, and #temp table identifiers. Use "Oracle (PL/SQL)" for q'[...]' quoting, "SQLite" for [brackets] and backticks without backslash escapes, or "BigQuery" for # comments, double/triple-quoted strings, and ARRAY or STRUCT syntax.
- Indentation: Format clauses, joins, CTEs, subqueries, and CASE blocks with "2 Spaces", "4 Spaces", or "1 Tab (\t)". Pick "Minify (1 Line)" to collapse whitespace and convert kept -- or # comments into /*...*/ blocks (while keeping T-SQL GO batch separators on their own lines).
- Keyword Case: Set SQL keywords and built-in functions to "UPPERCASE", "lowercase", or "Preserve Original" casing. Table names, AS aliases, dot-qualified columns, quoted identifiers, and string literals stay unchanged. Wrap reserved SQL keywords like check or default in quotes when using them as bare identifiers.
- Strip Comments: Remove -- and # comments, /*...*/ blocks, /*+...*/ optimizer hints, and /*!...*/ MySQL conditional comments outside string literals and quoted identifiers. When unchecked, comments stay intact while /*+...*/ optimizer hints and /*!...*/ MySQL conditional comments remain inline within clauses.
Key Features
- Structure SELECT, JOIN, CTE, subquery, CASE, and CREATE TABLE blocks with 2-space, 4-space, or tab indentation.
- Break AND/OR conditions onto new lines in WHERE, HAVING, and ON while keeping BETWEEN ranges inline.
- Minify SQL to one line, wrapping kept -- or # comments in /*...*/ so trailing clauses stay active.
- Change keyword case while preserving AS aliases, dot-qualified columns, table names, quoted identifiers, and UTF-8 strings.
- Support PostgreSQL $$ quotes, MySQL backticks, T-SQL [brackets] and GO batches, PL/SQL q'[...]' strings, SQLite, and BigQuery syntax.
- Flag exact line and column positions for unclosed quotes, comments, or mismatched parentheses and brackets locally in browser memory.
About This Online SQL Formatter
Supporting seven SQL dialects, this SQL formatter indents clauses, sets keyword case, strips comments, and minifies queries. Token-level scanning in browser memory preserves UTF-8 strings, quoted identifiers, column aliases, and exact decimal values.
Developed and maintained by the PU Tools team, this tool gives you fast, accurate, safe, and easy results in just a few clicks.
Common Use Cases
- ORM and Slow-Query Inspection: Expand single-line queries from Hibernate, Prisma, or database slow-query logs into readable clause blocks.
- Migration and Code Reviews: Standardize keyword case and indentation across schema migrations before committing .sql scripts to Git.
- Multi-Dialect Script Cleanup: Format queries using PostgreSQL $$ tags, T-SQL GO batches, or PL/SQL q-quotes without breaking syntax.
- Embedded Query Minification: Strip comments and minify multi-line SQL into one line for code strings or CLI commands.
Example Input and Output
Example Input
select c.customer_id, c.full_name, count(o.order_id) as order_count, sum(o.total) as spend_eur from customers c inner join orders o on c.customer_id = o.customer_id where c.city = 'Montréal' and o.note <> 'Café Crème 🚀' and o.total between 19.90 and 250.00 group by c.customer_id, c.full_name having count(o.order_id) >= 2 order by spend_eur desc limit 10;
Options used: SQL Dialect: Standard SQL (Auto) | Indentation: 2 Spaces | Keyword Case: UPPERCASE | Strip Comments: OFF
Example Output
SELECT c.customer_id, c.full_name, COUNT(o.order_id) AS order_count, SUM(o.total) AS spend_eur FROM customers c INNER JOIN orders o ON c.customer_id = o.customer_id WHERE c.city = 'Montréal' AND o.note <> 'Café Crème 🚀' AND o.total BETWEEN 19.90 AND 250.00 GROUP BY c.customer_id, c.full_name HAVING COUNT(o.order_id) >= 2 ORDER BY spend_eur DESC LIMIT 10;
Keywords convert to uppercase with two-space clause indentation, keeping BETWEEN inline alongside accents, 19.90, and emoji.
Frequently Asked Questions
Does this SQL formatter validate full database syntax or table schemas?
No, it structures query clauses and flags only unclosed quotes, comments, or mismatched parentheses and brackets. Checking full SQL grammar, column types, or table schemas requires validating the script in your database engine (for example, with EXPLAIN or SET PARSEONLY).
Why does minifying SQL convert -- and # comments into /*...*/ blocks?
Line comments run until a newline, so collapsing lines would comment out trailing FROM or WHERE clauses. Wrapping those in /*...*/ protects trailing SQL, while checking "Strip Comments" deletes all of them.
Will changing keyword case alter my table names, aliases, or string values?
No, case rules apply only to recognized SQL keywords and built-in function calls followed by parentheses. Your table names, column aliases, dot-qualified fields, quoted names, and UTF-8 string values remain untouched. If a bare column shares a reserved SQL keyword like check or default, quote it to preserve its casing.
When should I switch from Standard SQL to a specific database dialect?
Switch dialects when your script uses vendor-specific escapes, comments, or batch separators. Standard SQL already parses PostgreSQL $$ quotes and PL/SQL q'[...]' strings. Choose a dialect for MySQL backslash escapes and # comments, PostgreSQL nested comments, T-SQL GO batches and [brackets], or BigQuery triple-quoted strings.
When should I use SQL Formatter instead of Delimited List Converter?
Use this tool to indent clauses, standardize keyword case, or minify complete SQL queries and scripts. The Delimited List Converter converts raw ID or text columns into quoted SQL IN ('a', 'b') or numeric SQL IN (1, 2, 3) lists.
Is any part of my SQL query or schema script sent to a server?
No, token scanning and clause formatting run entirely in local browser RAM with zero network requests. Production queries, table names, and parameter values remain private on your machine without remote logging or storage.