SQL Formatter

Format or minify a SQL statement. Building a table from JSON stays on JSON to code

What this page does

A SQL statement on one line hides the clauses. This page puts each clause on its own line, or joins the tokens again. A CREATE TABLE from JSON stays on JSON to code.

How to use it

  1. Paste SQL. This page does not update as you type.
  2. Format runs once. Show Example fills select id, name from users where active = 1 and name = 'Ada' order by id limit 10.
  3. The result is SELECT then id, then name, FROM then users, WHERE then active = 1 then AND name = 'Ada', ORDER BY then id, LIMIT then 10.
  4. Minify writes one line: SELECT id, name FROM users WHERE active = 1 AND name = 'Ada' ORDER BY id LIMIT 10.
  5. Clear All empties both boxes. Copy Result copies the output.

Rules and edges

Known words are written in capitals. A string, including 'Ada', stays quoted. A line comment and a block comment are kept. AND and OR start an indented line. A comma in the select list starts a new indented line. An unfinished quote or an unfinished block comment stops with an error. This is a layout, not a database parser.

Where people use it

  • Reading a query copied from a log.
  • Putting a short statement back on one line.
  • Checking that a name inside quotes survives the format.

Questions

Does this build a CREATE TABLE?

No. JSON to code writes the table and the INSERT. This page only lays out SQL you already have.

Why is AND on its own line?

Each AND or OR in the example starts a new indented line under WHERE.