SQL Formatter
Paste your SQL query below to format it with proper indentation and syntax highlighting, or minify it into a single compact line.
How to Use the SQL Formatter
Writing clean, readable SQL is essential for maintaining database queries, collaborating with team members, and debugging complex data operations. This free online SQL formatter takes your raw or compressed SQL and transforms it into a well-structured, indented query with optional syntax highlighting and keyword uppercasing. Whether you are working with simple SELECT statements or complex multi-table JOINs, this tool makes your SQL easier to read and understand.
- Paste your SQL: Copy a query from your database client, application code, or log files and paste it into the input textarea above.
- Choose options: Select your preferred indent size (2 spaces, 4 spaces, or tabs) and optionally enable uppercase keywords for a consistent style.
- Click Format: The tool instantly reformats your SQL with proper line breaks, indentation, and syntax-highlighted output. Major clauses like SELECT, FROM, WHERE, and JOIN each start on their own line.
- Copy or minify: Use the Copy button to grab the formatted SQL, or click Minify to collapse the query into a compact single-line format.
SQL Keywords Recognised
The formatter recognises and handles line breaks around the following SQL keywords and clauses. Each major keyword starts on a new line, and sub-clauses are indented for clarity.
| Category | Keywords |
|---|---|
| Query | SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING, LIMIT |
| Joins | JOIN, ON, LEFT, RIGHT, INNER, OUTER, CROSS |
| Modification | INSERT, UPDATE, DELETE, SET, VALUES, INTO |
| Schema | CREATE, ALTER, DROP |
| Logic | AND, OR, CASE, WHEN, THEN, ELSE, END, UNION |
SQL Formatting Best Practices
Consistent SQL formatting improves readability and reduces errors in database development. Here are some best practices for writing clean SQL that work well with this formatter:
- One clause per line: Place SELECT, FROM, WHERE, and other major clauses on separate lines. This makes it easy to scan the structure of a query at a glance.
- Consistent keyword case: Use either all uppercase or all lowercase for SQL keywords. The uppercase convention is traditional and helps distinguish keywords from table and column names.
- Indent sub-clauses: Indent column lists, join conditions, and WHERE predicates to show the hierarchical structure of the query.
- Use aliases: Give meaningful short aliases to tables in JOINs to keep the query compact and readable.
- Comment complex logic: Add inline comments to explain non-obvious WHERE conditions, calculated columns, or business rules embedded in the query.
Minification for Deployment
When embedding SQL in application code, ORMs, or configuration files, a minified single-line format can be more practical. The minify function strips all unnecessary whitespace and line breaks, producing the smallest possible query string. This is also useful for logging and monitoring tools where multi-line queries may be truncated or hard to search. Switch between formatted and minified output with a single click to get the version you need.
Frequently Asked Questions
What SQL dialects does this formatter support?
This tool formats standard SQL syntax that is common across most database systems including MySQL, PostgreSQL, SQL Server, SQLite, and Oracle. It recognises all major keywords such as SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY, INSERT, UPDATE, DELETE, CREATE, ALTER, DROP, UNION, CASE, and more. While it does not parse dialect-specific extensions, the formatting works well for the vast majority of SQL queries.
Does the formatter change the meaning of my SQL query?
No. The formatter only modifies whitespace, line breaks, and optionally the case of keywords. It never alters table names, column names, values, operators, or the logical structure of your query. The formatted output is functionally identical to the input and will produce the same results when executed against your database.
When should I minify SQL instead of formatting it?
Minify SQL when you need to embed a query in a single line, such as in log messages, configuration files, URL parameters, or ORM query strings. Minified SQL is also useful when sharing queries in chat messages or documentation where vertical space is limited. For development, debugging, and code review, always use the formatted version for readability.
Can I change the indentation style?
Yes. Use the indent size dropdown to choose between 2 spaces, 4 spaces, or tab indentation. The formatter applies your chosen indent to sub-clauses and nested query blocks. Pick the style that matches your team's SQL coding conventions.