Skip to main content

SQL Formatter

Make a long query readable, using the syntax rules of your actual database.

Result
Your result will appear here

Formatted entirely in your browser — your SQL is never uploaded.

About this tool

The SQL formatter turns a query written as one long run-on into an indented statement where you can see at a glance which part is the filter, which is the JOIN, and how deeply the subqueries nest.

The usual situation is reading a query somebody else wrote, or one pulled out of an application log — those arrive as a single line hundreds of characters long. Formatting it first saves real time.

The setting to get right is the database dialect, because vendors quote identifiers differently: MySQL uses backticks, SQL Server square brackets, PostgreSQL double quotes. Choose the wrong one and an identifier containing a space can be misread.

Collapse mode exists for the return trip — putting a query back into code or a single-line input field. It strips comments, because a -- comment with no newline after it would comment out everything that follows.

How to use

  1. 1Paste a long or unreadable SQL statement into the input box
  2. 2Pick the database you actually use, such as MySQL or PostgreSQL
  3. 3Choose whether keywords should be upper case, lower case or left as typed
  4. 4Copy the result, or switch to collapse mode to get it back on one line

Frequently asked questions

Why do I have to pick a database?
Each vendor quotes identifiers differently: MySQL uses backticks, SQL Server square brackets, PostgreSQL double quotes. Pick the wrong one and the formatter can misread a column name and produce mangled output.
Does this run my SQL?
No. It is pure text formatting — there is no database connection, and your statement never leaves the browser.
Why does collapse mode remove comments?
Once everything is on one line, a -- comment swallows the rest of the statement and makes it unusable. Removing comments is the only way the collapsed output still runs.
Does it check my SQL for errors?
No. It formats only — it does not verify that the statement is valid or that the tables exist. Always test against the real database.
Does it handle long queries with nested subqueries?
Yes. Subqueries, JOINs, CASE expressions and CTEs are indented to show the nesting, which is exactly where formatting helps most when reading someone else's query.

Related tools

Code Beautifier

Re-indent HTML, CSS, JavaScript and SQL so it reads cleanly.

JSON Formatter

Format, validate and minify JSON — for developers.

Code Minifier

Shrink JavaScript, CSS, HTML and JSON files.

XML Formatter

Indent XML and check the structure is valid.