SQL Formatter and Beautifier

Format messy SQL into clean, readable queries, or minify it to a single line. This SQL formatter supports PostgreSQL, MySQL, SQL Server, Oracle, BigQuery, Snowflake and more dialects.

Result

                            
                            

Format SQL Queries Online

Queries copied from logs, ORMs or a colleague often arrive as one long line. This SQL formatter rewrites them with one clause per line, indented subqueries and consistent keyword case, so you can see the joins, filters and grouping at a glance and review changes more easily.

Choose the dialect your database uses so its special syntax is understood, from PostgreSQL casts to T-SQL brackets and BigQuery backticks. Switch to Minify to strip comments and whitespace for a compact one-line query. Formatting happens in your browser; your SQL is not sent to a server.

One-line SQL query formatted into readable linesselect * fromSELECT id,SQLFormatter

How to format SQL

  1. Paste your query into the SQL input. You can paste several statements separated by semicolons.
  2. Select the SQL dialect of your database, for example PostgreSQL or MySQL.
  3. Set the style: keyword case, function name case, indent width and whether AND/OR start or end a line.
  4. Copy the result. Click Minify instead when you need the query on one line, for example in a config file or a log search.

What the formatter changes

The formatter reads your SQL as tokens (keywords, names, strings, operators and comments) and prints them again with a consistent layout. It never changes what the query does:

  • Each main clause (SELECT, FROM, WHERE, GROUP BY, ORDER BY, LIMIT) starts on its own line, with its contents indented below.
  • Columns in the select list go on separate lines, and subqueries in parentheses are indented one more level.
  • Keywords and data types are converted to the case you choose. Table names, column names and string values keep their exact spelling.
  • Comments are kept when formatting. Minify removes -- and /* … */ comments, but keeps optimizer hints such as /*+ INDEX(t) */ and MySQL /*! … */ comments, because they affect execution.
  • Minify never touches text inside quotes, backticks or (for SQL Server) square brackets, so string values and quoted names stay exactly as written.

Supported SQL dialects

DialectTypical syntax it understands
PostgreSQL / Redshift:: casts, $$ dollar-quoted function bodies, ILIKE, RETURNING
MySQL / MariaDB`backtick` names, # comments, backslash escapes in strings
SQL Server (T-SQL)[bracket] names, TOP, @variables, GO
Oracle PL/SQLBEGIN … END blocks, CONNECT BY, :bind variables
BigQuery`project.dataset.table` names, STRUCT, ARRAY, QUALIFY
Snowflake, Spark, Hive, Trino, DuckDB, ClickHouseTheir own keywords and functions
Standard SQLGeneric ANSI SQL when your dialect is not listed

SQL style conventions

There is no single official SQL style, but most teams agree on a few rules, and the defaults here follow them:

  • Upper-case keywords (SELECT, JOIN) and lower-case names (users, created_at) make it easy to tell the language from your data model.
  • One column per line keeps version-control diffs small: adding a column changes one line.
  • Short, meaningful aliases such as u for users are fine in small queries; use full names in long ones.
  • Put AND and OR at the start of the line so conditions line up and are easy to comment out.
  • End each statement with a semicolon, especially in scripts with several statements.

Tips

  • Formatting failed? The most common cause is the wrong dialect, for example backticks with Standard SQL or $$ bodies with MySQL. Switch the dialect and try again.
  • Placeholders are kept when they match the dialect: ? for MySQL and SQLite, $1 for PostgreSQL, :name for Oracle and @name for SQL Server and BigQuery.
  • Templated SQL with {{ }} or ${ } variables may not parse; format the plain SQL first, then add the template tags back.
  • Minified SQL is for machines. Keep the formatted version in your repository and minify only where a single line is required.
  • Building types for the rows your query returns? Paste a JSON sample into the JSON to TypeScript converter or the JSON to Go converter.

Frequently Asked Questions

Common questions about formatting SQL.

Paste it into the SQL input above, choose your database dialect and copy the formatted result. It updates as you type.

No. Only whitespace, line breaks and the case of keywords change. Names, strings, numbers and operators stay the same, so the query returns the same result.

Standard SQL, PostgreSQL, MySQL, MariaDB, SQLite, SQL Server, Oracle PL/SQL, BigQuery, Snowflake, Redshift, Db2, Spark, Hive, Trino, ClickHouse, DuckDB, SingleStore, TiDB and N1QL.

It removes comments, line breaks and unnecessary spaces and puts the whole query on one line. Text inside quotes and optimizer hints are kept.

The formatter could not parse part of the query. Check for a missing parenthesis or quote, and make sure the selected dialect matches your database.

SQL keywords are not case-sensitive, so it is a style choice. Upper-case keywords are the most common convention because they stand out from table and column names.

No. Formatting and minifying run in your browser with JavaScript, so queries containing table names or data stay on your device.

It uses the open-source sql-formatter library (version 15.9.0), loaded from the jsDelivr CDN.