Format and beautify SQL queries with proper indentation.
The SQL Formatter reformats SQL queries with consistent indentation, line breaks and keyword case for standard SQL, MySQL, PostgreSQL, SQLite, SQL Server, Oracle, MariaDB, BigQuery and Snowflake. It understands strings, comments, function calls and nested queries, so the layout changes but the query does not.
Made for developers. Typical uses:
The one-line query select u.id,u.name,count(o.id) as orders from users u left join orders o on o.user_id=u.id where u.active=true group by u.id,u.name order by orders desc is laid out with SELECT and three indented columns, FROM users u with LEFT JOIN orders o ON o.user_id = u.id, then WHERE, GROUP BY and ORDER BY each on their own lines, with keywords in capitals.
It formats layout only; it does not validate table names or run the query. Very unusual vendor-specific syntax may be reported as an error.
Accuracy: Output is tested against known inputs. For production code, review the result and run your own tests.
Expert Tip
Choose the dialect of your database. Quoting rules and operators differ (backticks in MySQL, square brackets in SQL Server, :: casts in PostgreSQL), and the right dialect lets the formatter read them correctly.
The SQL Formatter turns a long query on one line into a consistently laid out one, with each clause on its own line, joins aligned and columns listed one per line. It supports standard SQL, MySQL, PostgreSQL, SQLite, SQL Server (T-SQL), Oracle PL/SQL, MariaDB, BigQuery and Snowflake. You also choose the keyword case (upper, lower or preserved) and the indent (2 spaces, 4 spaces or tabs). The query is processed in your browser, so your table and column names are not uploaded.
The one-line query select u.id,u.name,count(o.id) as orders from users u left join orders o on o.user_id=u.id where u.active=true group by u.id,u.name order by orders desc is laid out like this. SELECT is followed by three indented lines, u.id, u.name and count(o.id) AS orders. FROM, then users u, then LEFT JOIN orders o ON o.user_id = u.id. WHERE, then u.active = TRUE. GROUP BY with u.id and u.name on separate lines, and finally ORDER BY orders DESC. The keywords are in capitals and spaces are added around operators.
SQL is not one language. Quoting rules, function names and operators differ between databases: MySQL uses backticks around names, SQL Server uses square brackets, PostgreSQL has operators such as :: for casts and -> for JSON, and BigQuery has its own syntax for arrays and structs. Choosing the right dialect helps the formatter read those correctly. If your query uses vendor-specific syntax and fails in Standard SQL, try the dialect of your database.
Formatting is purely cosmetic. The tool does not run the query, check that tables and columns exist, or find logic errors, and it does not optimise performance. Very unusual syntax may be reported as an error. Strings, comments and function calls are preserved, so the query means the same thing before and after.
Consistent layout makes queries easier to review. Put each selected column on its own line, so that a diff shows exactly what changed, and keep keywords in capitals so that they stand out from table and column names. Before you share a query, remove any real customer data from literals. If your query output ends up as JSON, the JSON Formatter and JSON to CSV Converter help present it, and the YAML Formatter and XML Formatter tidy other formats.
The formatter handles one pasted script at a time. Procedural extensions, such as long stored procedures, can have unusual layout rules, and comments may be moved slightly to fit the layout. Always read the formatted query before you run it against a live database, and keep the formatted version in version control, so that later changes show up as small, readable differences instead of one long changed line.
Explore more in Developer Tools, read the guides and articles, or see how we test our tools.
Format, beautify, and validate JSON data with proper indentation.
Best for formatting minified JSON from APIs for readability
Format and beautify YAML files with proper indentation.
Best for fixing indentation in a docker-compose.yml before it fails to parse
Format and beautify XML data with proper indentation.
Best for making a minified API response readable before debugging it
Convert JSON data to CSV format.
Best for turning an API response into a spreadsheet for analysis
Convert CSV data to JSON format.
Best for turning a spreadsheet export into JSON for an API request