SQL Formatter

Paste a query to get it laid out clause by clause, with your choice of keyword case and indent, or squeezed onto one line. Pick your database for its own syntax.

Output
Keywords
Indent
More layout options
Layout
AND / OR
Blank lines between statements

Formatted in your browser; your SQL isn't uploaded or run. Strings, quoted names and comments are kept as written (Minify removes comments).

How to use it

  1. Paste your SQL or open a .sql file.
  2. Choose Format or Minify, your database's dialect, keyword case and indent.
  3. Copy the result or download it as a .sql file. If the query can't be read, the error shows the line and column.

Options

  • Keywords: UPPER (SELECT, COUNT()), lower, or as you typed them. Table and column names keep their case, except a name that is also an SQL word in the chosen dialect (such as date or user in Standard SQL), which changes with the keywords. Picking your database's dialect, or quoting the name, keeps it as typed.
  • Layout: standard puts each clause on its own line with its items indented; “keywords in a column” lines the clauses up in a left column.
  • AND / OR at the start or the end of a line, blank lines between statements, and tight operators (a=1).

Example

The one-line report query above, with Standard SQL, UPPER keywords and 2 spaces, comes out as:

SELECT
  c.id,
  c.name,
  COUNT(o.id) AS orders,
  SUM(o.total) AS revenue
FROM
  customers c
  LEFT JOIN orders o ON o.customer_id = c.id
  AND o.status = 'paid'
WHERE
  c.country = 'IN'
…

Every selected column gets its own line, each clause starts at the left, and count and sum become COUNT and SUM. Switch to Minify and the same query fits on one line again, with its comments removed.

Formatting uses the open-source sql-formatter library (MIT licence), which supports 20 dialects.

Frequently asked questions

Does formatting change what my query does?

No. Format changes only line breaks, indentation and, if you choose, the case of keywords and function names (SQL itself ignores that case). Strings, quoted names, numbers and comments are copied as written. Minify also removes comments, except optimizer hints (/*+ … */) and MySQL's /*! … */ code, which change how the query runs.

Why do I get an error for a query that works in my database?

Each database has its own syntax: T-SQL's [bracketed names] and TOP, PostgreSQL's $1 parameters and :: casts, MySQL's backticks and # comments, BigQuery's backtick table paths. Pick your database in the SQL dialect list and the formatter reads the query its way.

Is my SQL sent anywhere or run?

No. The query is laid out by code in your browser tab. It never reaches a database or our server, so table names and data in it stay private.

What does Minify do?

It puts the query on one line: comments go, runs of spaces and line breaks become one space, and spaces next to brackets, commas and = are dropped. A space stays wherever removing it would change the meaning, so a - -1 never becomes a--1, which would start a comment.

Can it format stored procedures?

It formats any number of statements separated by semicolons. Procedural blocks (BEGIN … END, IF, loops in T-SQL or PL/SQL) are laid out statement by statement rather than indented as nested blocks, so long procedures may need a little tidying by hand.

More text and developer tools

See all text and developer tools →

Can't find the tool you need? Tell us, and the most requested tools get built first.