SQL Formatter

Format tangled SQL into a readable, indented layout, or minify it into a single line. Supports MySQL, PostgreSQL, and Standard SQL.

๐Ÿ“ Drag & drop or click to select .sql file
Files are read in the browser only ยท Up to 8MB
Result

Tokenizer-based for safety. Contents within string literals ('...') and comments (--, /* */) are left as-is. Even if a word like select appears in a string, it won't be mistaken for a keyword, uppercased, or cause a new line.
Dialect differences โ€” MySQL: Backtick (`ident`) identifiers, backslash escapes in strings. PostgreSQL: Dollar-quoted strings ($$ ... $$), nested block comments. Standard: Double-quoted and bracketed identifiers. This is for token boundary detection, not full syntax validation.

What is the SQL Formatter?

SQL queries from logs or ORMs are often a single, long line, making them hard to debug. This formatter clarifies complex queries by breaking them down. It adds newlines for major clauses like SELECT, FROM, WHERE, and JOIN, and indents subqueries for a clear hierarchy. WHERE conditions with AND/OR and CASE statements are also split onto separate lines to reveal the logic. Crucially, it's built on a proper tokenizer, not simple text replacement. This means it never alters the contents of string literals or comments, so a word like 'select' inside a string is safe. Your data is also safe: all formatting happens in your browser and is never sent to a server.

How to use

  1. Paste your query into the `SQL Input` field or drag and drop a .sql file.
  2. Select the `SQL Dialect` (`Standard`, `MySQL`, or `PostgreSQL`) to handle syntax differences.
  3. Customize the output style using the `Keywords` case, `Commas` position, and `Indent` options.
  4. Click `Format (Pretty)` to get an indented, readable query, or `Minify` to compress it to one line.
  5. Use `Copy` to grab the result for your editor or `Save` to download it as a .sql file.

SQL Formatter guide

How this tool is used in real work, and what to watch out for.

Why Poor Formatting Is a Maintenance Cost

A 200-character query mashed into one line isn't just hard to read *now*. The real cost comes six months later. You misplace the cursor three times trying to add one condition, and your reviewer has to re-read the whole thing because they can't tell what changed.

When a query is broken down by clause, a git diff shows "one condition added to the WHERE clause." For a single-line query, the same change shows up as "this entire line was replaced." This can be the difference between a code review being possible or not.

That's why formatting isn't a matter of taste. If you're committing SQL to a repository, it pays for the team to standardize on one configuration. This tool's settings for keyword case, comma style, and indentation are saved in your browser, so once you set them, you'll get consistent output every time.

Leading vs. Trailing Commas: The Real Arguments

It might look like a style preference, but there are practical reasons why some developers insist on leading commas. Unlike JavaScript, SQL treats a trailing comma (after the last item) as a syntax error.

sql
-- Trailing comma: To add a column to the last line, you have to edit the previous line too.
SELECT
  id,
  name,        -- Forgetting to add a comma here is a syntax error
  email
FROM users

-- Leading comma: Just add the new line. It's also safe to comment out the last line.
SELECT
  id
  , name
  , email
FROM users
After item (a,)Before item (, a)
ReadabilityNatural โ€” the default for most teamsAwkward until you're used to it
Adding/deleting columnsRequires editing the comma on the previous lineJust edit the line in question
Diff noiseAdding one column changes 2 linesChanges 1 line
Commenting out the last lineLeaves a trailing comma, causing an errorSafe
There's no right answer. Just pick one. A mix of both styles in a single file is worse than committing to either. If you disable "One SELECT column per line," columns are kept on a single line, which may be preferable for short queries with just a few columns.

Why It's Crucial to Leave Strings and Comments Untouched

Many SQL formatters on the internet are built with simple regular expression replacements, like "replace `select` with `SELECT`" and "add a newline before `from`." This works fine for ordinary queries.

The problem arises when those keywords appear in your data. This tool works by splitting SQL into tokens and treating string literals and comments as single, indivisible units. This ensures that whatever content is inside them is preserved, character for character.

  • WHERE memo NOT LIKE '%select from where%' โ€” A regex-based formatter might break the query by inserting newlines into the middle of this string.
  • WHERE title = 'Order by ํŒ๋งค๋Ÿ‰' โ€” It's common for non-English text, like product names or post titles, to contain English keywords.
  • -- Do not add an order by here โ€” Uppercasing keywords inside comments just adds noise to code reviews.
  • SELECT '3''4' โ€” The tool correctly identifies the boundaries of values escaped with doubled single quotes.
Minify removes comments by default. This is because a line comment (`--`) extends to the end of the line; squashing everything onto one line would cause all subsequent SQL to be consumed by the comment. If you need to preserve them, enable "Keep comments when minifying." In this mode, a newline is kept after `--` comments, so the result will not be a perfect single line.

When Does Dialect Choice Matter?

The Standard, MySQL, and PostgreSQL choices aren't for syntax validation, but for correctly identifying token boundaries. Each dialect differs in what it considers an identifier quote versus a string literal. If your query doesn't use the features below, the "Standard" setting will work just fine.

DialectWhat it affectsWhen to choose it
MySQLBacktick identifiers, backslash escapes in stringsWhen using backticks or strings with backslash escapes.
PostgreSQLDollar-quoted strings ($$ โ€ฆ $$), nested block commentsWhen the SQL contains function bodies (e.g., CREATE FUNCTION โ€ฆ $$).
StandardDouble-quoted and bracketed identifiersWhen the other two don't apply; SQL Server style.
Clicking "Load Example" switches the dialect to MySQL and populates the editor with a query that includes keywords in strings, aliases, a CASE statement, and a subquery. It's a great way to see the formatter in action and help decide on your team's settings.

Limitations: This Is a Formatter, Not a Parser

Even SQL with syntax errors will be formatted on a token-by-token basis. This means a query that can't be executed might still come out looking pretty. However, the tool will display a warning if it detects broken token boundaries, like unclosed quotes or mismatched parentheses.

When pasting logs from an ORM, be mindful of placeholders. `?` and `$1` are left as-is, and queries logged with Python's `%s` will also be in their unfilled state. If you intend to run the query, you'll need to manually substitute the values from the log's parameter array.

The "Subquery Depth" statistic can be a good starting point for a review. If the depth is over 3 and mixed with `IN (SELECT โ€ฆ)`, the query is worth a closer look. However, this tool has no knowledge of the execution plan, so for performance analysis, you must always use your database's `EXPLAIN`.

Frequently asked questions

Will it change text inside strings or comments?

No. The tool tokenizes the SQL and treats strings and comments as single, untouchable blocks. It will not re-case or add line breaks based on keywords found inside them.

What's the difference between the SQL dialects?

The dialect choice affects how the code is tokenized. `MySQL` recognizes backtick identifiers, `PostgreSQL` handles dollar-quoted strings, and `Standard` supports quoted identifiers. This is not for syntax validation.

What happens to comments when I click `Minify`?

By default, comments are removed. To keep them, enable the `Keep comments when minifying` option. Line comments (`--`) will then be followed by a newline to prevent errors.

Are the SQL queries I enter sent to your server?

No. The entire formatting process runs in your browser. Your queries, including any sensitive data like table names or values, never leave your computer.