Paste tangled ORM-generated queries or slow-query log lines to format PostgreSQL, MySQL, or SQLite SQL with clean clause indentation, uppercase keywords, and safe preservation of quoted literals and comments.
ORMs like Prisma, Drizzle, and SQLAlchemy emit dense single-line queries with dozens of aliased joins when logging slow database statements. Reading a 900-character query straight out of pg_stat_statements hides missing index predicates and accidental cartesian joins.
This dialect-aware lexer tokenizes single-quoted literals, PostgreSQL dollar-quoted strings ($tag$...$tag$), MySQL backticks, and inline/block comments before formatting clauses, ensuring string values containing words like "select" or "from" are never altered.
SQL Formatting Mistakes That Break Queries
01. Mutating Keywords Inside String Literals
Regex-based formatters frequently uppercase words inside WHERE note = 'select from list', corrupting filter predicates. Tokenizing strings first guarantees literals remain byte-identical.
02. Dialect Quoting Differences Across Engines
MySQL quotes identifiers with backticks (`order`) and treats # as a line comment, whereas PostgreSQL uses double quotes ("order"), :: type casts, and ->> JSONB operators. Selecting the right dialect avoids token splits.
03. Minifying Queries Containing Line Comments (--)
Collapsing newlines without stripping -- comments turns everything after the first comment into commented-out dead SQL. Minify mode removes comments cleanly before joining tokens.
Supported Dialect Syntax Features
| Dialect | Identifier & Operator Handling | Example |
|---|---|---|
| PostgreSQL | Double-quoted identifiers, :: casts, ->/->> JSONB, $tag$ strings | payload->>'id'::text |
| MySQL | Backtick identifiers, \' string escapes, # comments | SELECT `order_id` FROM `orders` |
| SQLite | Standard ANSI quotes, CTEs, UPSERT ON CONFLICT clauses | ON CONFLICT(id) DO NOTHING |
| Minify Mode | Strips comments and collapses whitespace to one executable line | SELECT id FROM users WHERE active=1; |
Worked Engineering Examples
Untangling an Aggregation Query
select user_id, count(*) from events where kind = 'click' and created_at > '2026-01-01' group by user_id order by 2 desc limit 20;
SELECT user_id, count(*) FROM events WHERE kind = 'click' AND created_at > '2026-01-01' GROUP BY user_id ORDER BY 2 DESC LIMIT 20;
Major clauses anchor at column zero while SELECT columns and boolean predicates indent cleanly for rapid code review.
Protecting PostgreSQL Operators
select data->>'email' as email, amount::numeric from billing;
SELECT data ->> 'email' AS email, amount::numeric FROM billing;
Type casts (::) remain tightly bound to their operands while keywords switch cleanly to uppercase.
Frequently Asked Questions
Will formatting change text inside my WHERE string values?
No. The lexer isolates single-quoted strings, PostgreSQL dollar quotes, and quoted identifiers before keyword casing or clause line breaks run.
Can I format multiple semicolon-separated SQL statements at once?
Yes. Semicolons terminate each statement cleanly with a blank separator line and report the total statement count.
Does SQL Formatter connect to any database?
No. It performs purely syntactic tokenization and formatting locally in your browser without executing queries or sending table names anywhere.
When should I use Minify mode?
Minify mode is useful when embedding SQL inside environment variables, curl test fixtures, or compact log assertions where newlines are awkward.