Skip to content

SQL Formatter

Turn a one-line SQL query into clean, indented SQL

Loading the tool…

Breaks a long one-line SQL query into lines and indents each clause so it is easy to read, or squeezes formatted SQL back into one line. Use it to review a query taken from a log or from code, or to put it into documentation.

How to use

  1. Paste the query into "SQL input", or press "Open file" to load a .sql file. Several statements at once are fine.
  2. Choose your database under "SQL dialect". If you are not sure, leave it on "Standard SQL".
  3. Set "Keywords", "Indentation", "Commas" and "Line width"; the "Result" changes at once.
  4. Copy the result or press "Save as .sql". For a single line, switch "Mode" to "One line".

How it works

Questions and answers

Does formatting SQL change what the query does?
No. Only spaces, line breaks and keyword case change; quoted values and comments stay the same. To keep the keywords as you wrote them, set "Keywords" to "As typed".
Why do I get "Could not format"?
Look for an unclosed quote or bracket near the marked spot. Some syntax needs the right dialect: square-bracket names need SQL Server, and :: casts need PostgreSQL. "One line" mode works on any text.
Can I put commas at the start of each line?
Yes. Set "Commas" to "At line start" and the commas in lists such as SELECT columns move to the front of each line.
Why does a list in parentheses stay on one line?
Lists in parentheses shorter than "Line width" (50 characters by default) stay on one line. Lower the value to break more of them.