Skip to main content
Tools

SQL Formatter

Beautify messy SQL queries for MySQL, PostgreSQL, SQL Server, SQLite, BigQuery and more, with keyword case and indent options.

Keyword case
Indent
AND/OR position

0 characters · 1 line

How to use

  1. Paste a query into “Input SQL”, or click “Example” to try it out.
  2. Choose your database dialect (such as MySQL, PostgreSQL or SQL Server); the result is formatted as you type.
  3. Adjust keyword case, indent, alignment and the position of AND/OR to taste. Your settings are remembered in this browser.
  4. Click “Copy” or “Download” to grab the formatted SQL. If the syntax is invalid, the error position is marked below the input.

FAQ

Is my SQL uploaded?

No. Formatting happens in your browser; your SQL is never sent to a server and nothing connects to your database.

Why choose a dialect?

Databases differ in syntax: MySQL quotes names with backticks, SQL Server uses [brackets] and PostgreSQL has :: casts. With the right dialect, the formatter recognizes that syntax and its reserved words; database-specific syntax may cause errors with “Standard SQL”.

What does “Keyword case” change?

Keywords such as SELECT and FROM, function names such as COUNT() and data types such as INT and VARCHAR. Table and column names, strings and comments are left as they are.

Can formatting change what my query does?

No. Only whitespace, line breaks, indentation and keyword case change; nothing is added, removed or reordered. Strings and quoted names (including non-English names) are kept exactly.

What should I do about “… is not expected here”?

The formatter could not read the syntax at the marked position. Common causes are unmatched parentheses, a missing closing quote or syntax the chosen dialect does not support. Make sure the right dialect is selected, then check the syntax near the mark.

Can commas go at the start of lines?

The formatting library used here (sql-formatter) no longer supports leading commas, so commas always end the line. The “Tabular” alignments can make long column lists easier to read.

Why does the first use take a moment?

The formatter and each dialect’s keyword lists are fairly large, so they load after the page opens, and only for the dialect you choose. Once loaded, formatting is instant.

Keyboard shortcuts

/
Search tools (on a tool page, goes to the homepage search)
?
Show this help
Esc
Close a dialog
Ctrl / ⌘Enter
Run the tool's main action (where supported)

While you type in a text field, / and ? are typed as usual.