toolbench.onlineby Valkyron Labs

Developer

SQL Formatter

Reflow SQL by uppercasing known keywords and breaking before major clauses, or minify it to one line. Comments survive format mode. Copy or download.

SQL Formatter

Uppercase SQL keywords and break before major clauses, or minify.

Paste SQL to format or minify.

SQL output

—

Output actions

Ready to copy output once your result appears.

Keyword breaks and one-line minify

SQL Formatter retokenizes a statement. Format mode uppercases a fixed keyword list, starts a new line before selected clauses, and breaks after commas and parentheses. Minify mode joins the same tokens into one line, drops line comments, and leaves keyword case as it was. Neither mode opens a database or checks that the statement is legal.

How to reflow a statement

  1. 1Paste into SQL input. Format is selected at open. Minify is the other button. Switching the button does not reflow the text already in the result.
  2. 2Press Format SQL or Minify SQL. The label on the button follows the mode. Load sample fills a longer SELECT with a left join. The status reads SQL formatted. or SQL minified.
  3. 3Copy output, or Download output, which saves sql-formatter.sql. An empty field throws Paste SQL to format. or Paste SQL to minify. and clears the panel.

What it is good for

  • • Splitting a one-line SELECT so FROM, WHERE, and ORDER BY sit on their own lines.
  • • Reading an INSERT that was generated as a compact values list.
  • • Collapsing a formatted statement to one line without the -- comments.

Example

Input

select id, name from users where active = 1 order by name

Output

SELECT id,
name
FROM users
WHERE active = 1
ORDER BY name

Format mode. The comma ends the id line and name starts at column 0. desc would have stayed lowercase. order by is rewritten as ORDER BY.

Which words move to their own line

The tokenizer skips whitespace, keeps a quoted string together (a doubled quote inside quotes is still part of the token), keeps a -- comment through the end of the line, and treats parenthesis and comma as their own tokens. Other characters gather into words. Format then uppercases a word only when the uppercase form is in a fixed set: SELECT, FROM, WHERE, AND, OR, JOIN, the join directions, ON, GROUP, BY, ORDER, HAVING, LIMIT, OFFSET, INSERT, INTO, VALUES, UPDATE, SET, DELETE, CREATE, TABLE, ALTER, DROP, AS, DISTINCT, UNION, ALL, CASE, WHEN, THEN, ELSE, END, WITH, NOT, NULL, IN, IS, LIKE, BETWEEN, EXISTS, and RETURNING. count and desc are not in that set, so they stay as typed.

A new line starts before SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING, LIMIT, OFFSET, the join forms, UNION, VALUES, SET, and RETURNING, when the current line already has text. GROUP BY and ORDER BY are glued into one token and consume the following BY. LEFT JOIN and the other direction-plus-JOIN pairs are glued the same way. AND and OR are uppercased but do not force a break, so they stay on the WHERE line. A comma ends the line immediately. An opening parenthesis ends the line and increases the indent by two spaces. A closing parenthesis ends the line, decreases the indent, and is written at the new indent.

Format, minify, and the sample button

The sample input select id, name from users where active = 1 order by name becomes SELECT id, then name on the next line with no indent, then FROM users, WHERE active = 1, and ORDER BY name. There is no aligned column under id. Minify of that result is SELECT id, name FROM users WHERE active = 1 ORDER BY name because minify joins tokens with spaces and then tidies comma and parenthesis spacing. Minify does not uppercase. If you minify the original lowercase sentence, select stays select. Line comments that start with -- are kept on their own line in format mode and deleted in minify mode.

Load sample inserts a query that selects u.id, u.name, and count(o.id) as orders, with a left join, a where, a group by, an order by, and a limit. Format will uppercase the known words, break before LEFT JOIN, WHERE, GROUP BY, ORDER BY, and LIMIT, and leave count and desc in their original case. AS becomes AS. The button only fills the field. You still press Format SQL.

Reading a generated INSERT

The CSV to SQL tool writes INSERT statements. This formatter does not know that tool's dialect settings. It only retokenizes whatever you paste. An INSERT with a column list and two value rows breaks before VALUES, indents inside each parenthesis, and puts a comma at the end of a row's closing parenthesis line. INSERT and INTO stay on the opening line because INTO is uppercased but is not a break keyword. A trailing semicolon is its own token, so it appears with a space before it: ) ;. Quoted identifiers such as "records" stay quoted and are not uppercased, because the quotes make one token. A doubled apostrophe inside a string, as in 'O''Neil', stays inside that token.

The guide Prepare CSV for PostgreSQL, MySQL or SQLite INSERT covers headers, NULL policy, and dialect quoting before any SQL exists. Formatting is the later reading step. This page will not connect to a server, create a table, or decide whether a value should be NULL. A statement that is missing a parenthesis is still printed. The formatter is not a parser with error positions.

Comments, empty input, and the file name

A -- comment is taken from the two dashes through the end of the line, without the line break. Format prints it on its own line at the current indent. Minify drops every token that starts with --, so the comment text is gone and the words on either side are joined. Block comments that use slash-star are not special. They are ordinary word characters and will sit inline.

An empty or all-whitespace field fails before tokenization. Format says Paste SQL to format. Minify says Paste SQL to minify. The panel is cleared. Download output uses the name sql-formatter.sql because the slug contains sql. The file is still the reformatted text, not a checked script. Copy output copies that same text. Neither control runs the statement.

Rules and limitations

Format uppercases the fixed keyword list, breaks before the clause set, breaks after commas, and indents two spaces per parenthesis level. Words outside the list, including count and desc, keep their case. Quoted strings and quoted identifiers stay intact.

Minify joins tokens, drops -- comments, and does not uppercase. Slash-star comments are not recognized. Empty input errors and clears the result. Download output names the file sql-formatter.sql. The statement is not executed.

Common problems

If name sits at the left margin under SELECT id, that is the comma rule, not a lost indent. The comma closes the line and the next word starts a new line at the current depth.

If minify left select in lowercase, that is expected. Uppercase is a format-mode behavior. Run Format SQL when you want the keyword list uppercased.

Frequently asked questions

Does format check SQL syntax?

No. It retokenizes and inserts line breaks. A missing parenthesis or a nonsense word is still printed. Nothing is sent to a database.

Why is desc still lowercase?

desc and count are not in the keyword set. ORDER BY is uppercased because ORDER and BY are in the set and are combined into one break.

What happens to -- comments when I minify?

They are removed. Format mode keeps each -- comment on its own line. Slash-star comments are not treated as comments in either mode.

What is the download file called?

sql-formatter.sql. It contains the formatted or minified text from the last successful run.

Is my input uploaded or saved?

This tool processes input in your browser and does not save its input or output to local storage. Copy and download are user actions. The site uses analytics for page and action metadata and may use ads and production error reporting; see the Privacy Policy for those services.

About this tool

Maintained by Toolbench. For large or deeply nested files, split the input into smaller batches and verify representative results before using them in an import or application. Report reproducible problems through Contact.

Guides

Next steps

Related tools

Continue the workflow with nearby tools for formatting, publishing, debugging, or follow-up checks.

Trust

Policies, contact, and answers

Clear pages for how Toolbench works, what we collect, and how to reach us.