SQL Formatter (Beautify, Minify to One Line, IN Clause Generator)

Completely free, with no extra charges. You may freely use the output commercially.

About this tool

Paste SQL that is crammed onto a single line, or hard-to-read SQL copied from logs or ORM output, and this tool reformats it with line breaks and indentation for each clause such as SELECT, FROM and WHERE. Subqueries, WITH clauses (CTEs), CASE expressions, JOINs and AND/OR conditions are indented level by level, so you can follow which condition applies where. You can choose uppercase or lowercase keywords, the indent width, and whether commas go at the end or the start of a line. It also works the other way round, compressing formatted SQL into a single line so you can embed it in program code.

There is also an IN clause generator that turns a list of IDs or codes copied from an Excel column into WHERE code IN ('A001', 'A002', …). It can add or omit quotes, remove duplicates, and split the list to stay within Oracle's 1,000-item limit. All processing happens entirely in your browser (no data is sent).

Format SQL

Build an IN clause from a list

Select a column in Excel or a spreadsheet, copy it and paste it here as is. Each line is read as one value; leading and trailing spaces and empty lines are removed.

How to use

  1. Paste your SQL into “SQL to format”. The “Formatted SQL” box below updates as soon as you type.
  2. If you use MySQL # comments or SQL Server [column name] identifiers, set “Database” accordingly.
  3. Match keyword case, indentation and comma position to your team's style, then click “Copy result”.

The default example is a one-line query that totals the number of orders and the order amount for each member. Once formatted, the SELECT columns appear one per line, and the LEFT JOIN condition, the three WHERE conditions and the subquery inside NOT IN are each indented to their own level. Short parenthesized groups such as (u.plan = 'pro' OR u.plan = 'team') stay on one line so the grouping remains easy to see.

Formatting rules

  • Line breaks per clause: clauses such as SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, INSERT INTO, VALUES, UPDATE, SET, DELETE FROM and WITH start a new line, and their contents are indented one level. UNION, UNION ALL, INTERSECT and EXCEPT are placed at the same level as the surrounding SELECT statements.
  • Columns and conditions: comma-separated lists of columns or values are broken onto one item per line. Conditions in WHERE, HAVING and JOIN … ON are broken before AND and OR. The AND in BETWEEN 20 AND 30 is not broken. JOINs are placed on separate lines within FROM.
  • Parentheses: short parentheses that contain no subquery or CASE (function arguments, IN values, grouped conditions) stay on one line. Subqueries starting with SELECT, parentheses containing CASE, and long parentheses are broken across lines and indented one level deeper, with the closing parenthesis returning to the level of the line that opened it.
  • CASE expressions: each WHEN and ELSE goes on its own line, and END is aligned with CASE.
  • Multiple statements: statements separated by semicolons are formatted in order with one blank line between them.

Only whitespace, line breaks, indentation and the case of keywords and function names are changed. Table names, column names, strings, numbers and comment text are left exactly as typed. Words that are also used as column names, such as DATE and YEAR, only have their case changed when they are followed by parentheses as a function. This keeps the result safe on databases where table names are case-sensitive, such as MySQL on Linux.

Putting SQL on one line (minify)

Setting “Output” to “One line” removes line breaks and indentation and collapses the space between words to a single space. Use it when you want to put SQL on a single line in Java or Python code, a shell script or a configuration file. A -- comment runs to the end of the line, so keeping it on a single line would turn all the SQL after it into a comment as well. For that reason, when comments are kept, a line break is inserted after them. If you want truly one line, turn on “Remove comments”.

Building IN clauses and things to watch out for

Paste values one per line into “Build an IN clause from a list” and you get code IN ('A001', 'A002', 'B015', 'O''Brien'). In the default example A001 appears twice, so one is removed, and the ' in O'Brien is doubled to '' as SQL requires.

  • Numbers and strings: with “Auto”, values are left unquoted when every value consists of digits only. Values that start with 0, such as 001, are quoted as strings, because converting them to numbers would drop the leading zero and give a different value. For values made of digits that are stored in a string column, such as product codes or postal codes, choose “String”.
  • Oracle's 1,000-item limit: Oracle Database allows at most 1,000 values in a single IN list; anything over that fails with ORA-01795 (maximum number of expressions in a list is 1000). If you set “Split by count” to every 1,000, the output is split like (code IN (…) OR code IN (…)). With NOT IN the parts are joined with AND.
  • NOT IN and NULL: rows whose column value is NULL match neither IN nor NOT IN. If you also want rows with NULL, add OR code IS NULL.
  • Backslashes: with default settings MySQL and MariaDB treat \ as an escape character in strings, so for values like C:\temp turn on “Change \ to \\”. In PostgreSQL (default settings), Oracle and SQL Server, \ is an ordinary character, so leave it off.

When you have more than a few thousand values, loading them into a temporary table and joining it is easier to manage than a longer IN clause, both in terms of SQL length limits and execution plans.

Differences between databases

The formatting rules are the same for every database, but strings and comments are read differently, which is why you can switch the “Database” setting.

  • MySQL / MariaDB: text from # to the end of the line is a comment, \' inside a string is an escaped quote, and "…" is treated as a string. `column` is read as an identifier in every setting.
  • SQL Server: square brackets such as [Order ID] are treated as identifiers. In the other settings they are read as array subscripts such as arr[1].
  • PostgreSQL: $1 parameters, $$ … $$ dollar quoting, E'…' escape strings, ::int type casts and JSON operators such as ->> are supported.

Placeholders ?, :name, @name and $1 are all left as they are.

Error messages and what this tool cannot check

Unclosed quotes or comments, unmatched parentheses, CASE without END, and full-width spaces or full-width symbols outside strings are reported below the formatted result. Full-width characters such as the ideographic space (U+3000) and fullwidth punctuation like ,()= can slip in when SQL is copied from Chinese, Japanese or Korean documents or typed with a CJK input method. For example, if a full-width space follows a column name written in CJK characters, some databases do not treat it as whitespace, which leads to syntax errors or unintended column names. This tool formats full-width spaces outside strings as ordinary spaces.

This tool does not execute SQL to check its syntax. It does not verify that tables or columns exist or that a function is available in your database, so run the formatted SQL against your actual database to confirm it. Definition statements such as CREATE TABLE and ALTER TABLE are formatted only to the extent of putting each item inside long parentheses on its own line.