Web Development & SEO

SQL Query Formatter

This SQL formatter breaks a query into its clauses, puts each clause keyword (SELECT, FROM, WHERE, JOIN…) on its own line, lists columns and conditions one per line, and indents sub-queries one level deeper. Strings, quoted names and comments are copied exactly, so the formatted query returns the same results as the original.

Last updated Reviewed by ToolsPulse Hub

Style
Output—
Statements—
Characters—

How to use the SQL formatter

  1. Paste a query from your code, a log file or a database console. Several statements separated by semicolons are fine.
  2. The formatted version appears on the right as you type.
  3. Choose whether keywords are uppercased, and pick 2 spaces, 4 spaces or tabs.
  4. Switch to “One line” to collapse a query for a log message, a code string or a chat message.
  5. Copy the result or download it as a .sql file.

How the formatter decides where lines break

The query is first split into tokens: words, numbers, operators, strings, quoted identifiers and comments. The formatter then applies a small set of layout rules, the same ones many teams use in code review:

SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, INSERT INTO, VALUES, UPDATE, SET, DELETE FROM, WITH, UNION → new line at the current level, contents indented below JOIN types (LEFT, INNER, CROSS…) → new line, ON stays on the same line AND / OR in WHERE, HAVING and ON → new line, indented commas in column and value lists → new line per item ( SELECT … ) → sub-query indented one more level BETWEEN a AND b → kept together on one line

Worked example

This one-line query from an application log is hard to scan:

select o.id, c.name, o.total from orders o join customers c on c.id = o.customer_id where o.total > 100 and o.status = 'shipped' order by o.total desc

Formatted, it becomes 12 lines:

SELECT
  o.id,
  c.name,
  o.total
FROM
  orders o
JOIN customers c ON c.id = o.customer_id
WHERE
  o.total > 100
  AND o.status = 'shipped'
ORDER BY
  o.total DESC

Each filter now sits on its own line, so it is easy to see that the query keeps only shipped orders over 100.

Why formatting matters for SQL

SQL is usually written once and read many times: in code review, while debugging a slow query, or when someone new inherits a report. One condition per line makes mistakes visible, such as an OR that should have been wrapped in brackets with the AND next to it. Version control also benefits, because adding a column changes one line instead of rewriting a long one.

The formatter does not check that the SQL is valid or run it. Text inside quotes, including words like “from” or “select”, is never touched.

Common table expressions

Queries that start with WITH define named sub-queries, called CTEs, before the main SELECT. The formatter indents each CTE body one level deeper and closes it on its own line, so you can read a complex report from top to bottom like a series of steps. Load the sample to see one.

Key features

  • Works with standard SQL as used by MySQL, PostgreSQL, SQL Server, SQLite, Oracle and BigQuery.
  • Uppercase keywords or keep your own capitalisation.
  • Indented sub-queries, CTEs and JOIN conditions.
  • One-line compact mode that also removes comments.
  • Your queries and table names stay in your browser.

Practical use cases

Reading ORM output. Frameworks such as Django, Rails, Laravel and Hibernate log long generated queries. Paste one here to see the joins and filters clearly.

Code review. Format queries before committing so reviewers see a clean diff.

Sharing with colleagues. Use compact mode to paste a query into a ticket or chat, and formatted mode when you need someone to check its logic.

Frequently asked questions

This tool runs entirely in your browser and is provided as-is. See our terms and privacy policy.