SQL formatter

The SQL formatter makes SQL queries readable using the syntax of the database type you choose, with indentation by nesting level, and minifies the code by removing extra whitespace.
Input
Paste one or more queries, and choose Format or Minify. Up to 512 KB.

Formatting and minifying run in the browser. Please enable JavaScript.

Everything runs in your browser; nothing is sent or stored.

  • Paste one or more queries into the "SQL code" field, and click "Format" or "Minify".
  • In the "Database type" field, choose the database your query runs on. Changing the type processes the code again.
  • In the "Keywords" field, choose upper case, lower case, or "Unchanged".
  • Under "Indent", choose "2 spaces", "4 spaces", or "Tab", and in the "Blank lines between queries" field, choose the number of lines.
  • Below the result, the tool shows the size in bytes before and after processing, and when you minify, the percentage saved. Strings, quoted names, and comments are kept as they are. Minifying removes only extra spaces and line breaks: a line comment (-- or #) keeps the line break after it, and the SQL Server GO command stays on a line of its own.
  • Copy the result by clicking "Copy".

Formatting SQL queries for reading

Queries written in application code, found in a slow query log, or generated by an ORM library often come on one long line. When you format a query, each main clause, such as SELECT, FROM, WHERE, and ORDER BY, starts on a new line, each column in a list appears on its own line, and a subquery is indented. In a formatted query, it is easy to see which tables are joined, which conditions filter the rows, and where a condition is missing.

Why choosing the database type matters

Each database has its own syntax. In MySQL and MariaDB, table and column names are written between backticks, such as `users`, and the # character starts a comment. In SQL Server, names are written in square brackets, such as [users], and in PostgreSQL, a function body is often written between two $$ signs. The SQL formatter recognizes keywords, strings, and comments by the type you choose, so formatting code written for another type may fail or give a wrong result. The "Keywords" option also changes the case of function names and data types, while table and column names, strings, and comments stay as written.

When to minify SQL

When you minify a query, every run of spaces and line breaks is shortened to a single space, and extra spaces next to commas and parentheses are removed. A minified query is handy for pasting as a single string into code, a configuration file, or a command line. Minifying does not remove comments: a line comment, which starts with two hyphens, keeps the line break after it, so that the rest of the query does not become part of the comment.

SQL snippets and their formatted and minified result

  • [users]with the SQL Server (T-SQL) type, the square brackets enclose a table name. With the Standard SQL type, formatting shows an error.
  • # notewith the MySQL, MariaDB, and BigQuery types, the # character starts a line comment. With the Standard SQL type, formatting shows an error.
  • 'it''s'two single quotes inside a string stand for one single quote, and the string is kept as it is.
  • count(*)when upper case is selected, the function becomes COUNT(*).
  • GOwith the SQL Server (T-SQL) type, the word GO, which separates batches of statements, stays on a line of its own, even when you minify.
  • -- notewhen you minify, the comment is kept, along with the line break after it.

SQL formatter FAQ

Why does formatting show an error?

Formatting shows an error when part of the code does not match the syntax of the selected database type. The error message usually shows the line and the column of the error. To fix the error, choose the database your query runs on in the "Database type" field.

Does the SQL formatter check that the query is valid?

No. The SQL formatter does not check that the query is valid, does not run it, and does not check that the tables and columns exist. Even a misspelled keyword may pass formatting without a message, so test the query in the database itself.

Can minifying change the meaning of the query?

No. Minifying changes only spaces and line breaks outside strings, quoted names, and comments, so the meaning of the query stays the same. When the code has a string, a quoted name, or a comment that is not closed, the tool shows an error with the line number, and the code is not minified.

Can I format a procedure with the DELIMITER command?

No. Code with the DELIMITER command cannot be formatted or minified. DELIMITER is a command of the MySQL and MariaDB client program, and it is not part of SQL syntax. To process a procedure, remove the DELIMITER lines and the delimiter they define, and add them back after processing.

Does the "Keywords" option also apply to minifying?

No. The "Keywords" option applies only to formatting, and minifying keeps all letters as they were written. Changing this option, the indentation, or the number of blank lines formats the code again.

What is the maximum size of a SQL query I can format?

The input can be up to 512 KB, and the result can be up to 2 MB.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement