🔒 Processed locally in your browser — no data uploaded
← Back to Home

SQL Formatter

Input SQL
Result

What the SQL Formatter does

This page turns a single-line or badly wrapped SQL statement into something readable. Clause keywords are recognised regardless of case, moved onto their own lines, and the clauses that follow the main verb are indented by two spaces, so the structure of the query becomes visible at a glance. The page never connects to a database and never parses your statement as SQL; it works on the text alone.

Compress goes the other way. Every run of spaces, tabs and newlines collapses to a single space and the ends are trimmed, giving a statement you can drop into a string literal, a URL parameter or a shell command. A practical sequence is to compress a paste out of a chat message, copy that single line back into the box, and format it to restore the structure the message lost.

How to use it

  1. Paste the statementDrop a SELECT, INSERT, UPDATE or CREATE TABLE into the box, one statement per run. A sample query is loaded already, so you can watch what the buttons do before touching your own SQL.
  2. Format or compressFormat spreads clauses over lines with two-space indents. Compress folds whitespace down to single spaces and trims the two ends. Neither button edits the text sitting in the input box, only the result area below it.
  3. Format a copyBoth buttons read the input box every time, so to rebuild structure after compressing, paste the single-line result back into the box and press Format again. The result area is plain text, so dragging a selection out of it works normally.
  4. Clear and repeatClear empties the input and the result together, and nothing is stored between runs. If a formatted statement matters to you, copy it out before closing the tab or pasting a different query into the same box.

Options and formats

FormatInserts a line break before every recognised clause keyword and indents FROM, WHERE, AND, OR, ON, JOIN, GROUP BY, ORDER BY, HAVING, LIMIT, SET and OFFSET by two spaces beneath the main statement verb.
CompressCollapses all whitespace to one space and trims the outside. The usual use is pasting a query into a config value, a JSON string or a command line where a literal newline in the middle would break things.
Keyword detectionMatching ignores capitalisation, so select and WHERE are both caught. Around sixty keywords are on the list, covering join variants, aggregates, CASE and WHEN, and the IS NULL family, and the words you typed keep their original case.
Plain-text resultThe output is ordinary text rather than syntax-highlighted code, with indentation stored as leading spaces. That survives a copy into an editor, a code block or a ticket comment, while a rich-text field may collapse the spacing.

Things to watch out for

Strings and comments get movedThis is a keyword-driven rewriter, not a SQL parser, so a clause word inside a quoted value or a comment still triggers a line break. Check any statement whose string literals contain text such as 'order by date' or a note beginning with SELECT.
Indentation does not nestOnly two levels exist, zero for the main statement verb and one for everything that follows. A parenthesised subquery receives the same two-space indent as its parent, so deeply nested queries still need manual tidying after formatting.
Compress flattens quoted spacingRuns of spaces inside string literals collapse along with everything else, so a value entered as 'two spaces' comes back as 'two spaces'. Keep the original text until you have compared the two, and do not compress statements holding padded fixed-width values.
One statement per runA script with several statements separated by semicolons is treated as one block. The second statement inherits indentation decisions made for the first, which usually reads worse than the original, so split multi-statement files before formatting them here.

Frequently asked questions

Does formatting change what the query does?

No. The tool only inserts newlines, indentation and spaces between existing tokens, so the meaning the database sees is unchanged. The single exception is whitespace inside a quoted value, which Compress collapses. Compare a formatted run against your original before committing SQL to a repository.

Which SQL dialects are supported?

The keyword list is generic ANSI-style vocabulary, which covers the clauses the formatter knows how to pull apart. Dialect-specific constructs such as Postgres :: casts, MySQL backticks or T-SQL bracket identifiers are not on that list, so those pieces stay where they are instead of being split onto their own lines.

Why did my keywords stay lowercase?

Detection ignores case, but the matched text is written back exactly as you typed it. The tool moves keywords rather than restyling them. If you want uppercase keywords throughout, do a whole-word find and replace in your editor after formatting; that combination is usually faster than typing the query again.

Can it repair a broken query?

No. Reformatting never fixes syntax. A statement with a missing comma or an unclosed parenthesis comes out looking tidy and still fails when the database reads it. Run the formatted text through your database client to confirm it executes before trusting the layout.

What happens with a very long query?

Everything runs in the page with ordinary string operations, so a query containing hundreds of keywords returns a result quickly. Extremely large scripts produce a long result area that is slow to scroll through. Splitting a dump into single statements keeps the page responsive and the output easier to read.

Is the output safe to paste into a shell?

Only after compressing it. A formatted statement holds literal newlines, and pasting that into a terminal sends each line as a separate command. Compress first, then wrap the single-line result in quotes and check that the shell will not expand characters such as asterisks or semicolons inside it.