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
- Paste the query into "SQL input", or press "Open file" to load a .sql file. Several statements at once are fine.
- Choose your database under "SQL dialect". If you are not sure, leave it on "Standard SQL".
- Set "Keywords", "Indentation", "Commas" and "Line width"; the "Result" changes at once.
- Copy the result or press "Save as .sql". For a single line, switch "Mode" to "One line".
How it works
- Formatting uses the sql-formatter library (MIT licence) and offers 20 dialects, including MySQL, PostgreSQL, SQL Server, Oracle PL/SQL, SQLite and BigQuery.
- Only spaces, line breaks and keyword case change; text inside quotes and comments stays as it is. In "One line" mode, -- comments are turned into /* */ comments so the query still works.
- Your SQL is handled only in this browser. It is never run or sent to a server. Files over 20 MB cannot be opened.
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.