SQL formatter and minifier
Format SQL so each clause sits on its own line, or minify it onto one line, for MySQL, PostgreSQL, SQLite, SQL Server and BigQuery.
Code examples
Copy and paste these into your own project. Each one uses only the language's standard library, so there is nothing to install.
const sql = 'SELECT\n name,\n email\nFROM\n users\nWHERE\n active = 1'
console.log(sql.replace(/\s+/g, ' ').trim())What it does
Formats SQL so each clause sits on its own line, or squeezes it onto a single line. It reads MySQL, PostgreSQL, SQLite, T-SQL (SQL Server) and BigQuery as well as standard SQL, and a syntax error comes with its line and column.
How to use it
- Paste SQL, or drop a
.sqlfile. - Choose Format or Minify, then the dialect, the keyword case and the indent.
- Copy or download the result, or use Use output as input to turn it into the next input.
Examples
Formatting select id,name from users where active=1 puts SELECT, FROM and WHERE on their own lines with the columns and conditions indented beneath them. Minifying the formatted query gives SELECT id,name FROM users WHERE active = 1.
Good to know
- Only whitespace and, if you choose, the case of keywords change. Names, strings and numbers stay as typed.
- Minify removes
--and/* */comments but keeps MySQL/*! */and optimizer/*+ */comments, which a database acts on. - Pick the dialect your database speaks: backticks are MySQL, square brackets are T-SQL and
::casts are PostgreSQL. - The formatter loads the first time you format, so that first result can take a moment. Minify needs no download.
Why does the dialect matter?
Databases write names and operators differently: MySQL quotes names with backticks, T-SQL with square brackets, and PostgreSQL has :: casts and $$ strings. With the wrong dialect, valid SQL can fail to parse or be laid out oddly.
Frequently asked questions
Does formatting change what my query does?
No. Only whitespace and, if you choose, the case of keywords change. Names, strings and numbers stay as typed. Minify drops comments, except the MySQL /*! */ and /*+ */ ones, which the database can act on.
Why does it report a syntax error?
The formatter has to read the query to lay it out, so it stops at the first token it cannot parse and gives its line and column. Check the dialect first, then the spot it points at: a missing comma, quote or closing parenthesis.
Does this tool send my SQL anywhere?
No. Everything happens in your browser. What you type is never sent to our servers.