SQL Formatter

Paste a SQL query and get it back with each clause on its own line, select columns listed one per line, conditions lined up and subqueries indented. Choose upper-case or lower-case keywords and the indent size, or switch to minify to squeeze the query onto one line. The tool reads strings, quoted names and comments as units, so they are never changed. It works with common SQL used by PostgreSQL, MySQL, SQL Server and SQLite.

What you enter

What you get

How it works

  1. Paste one or more statements into the box. Statements ending in a semicolon are separated by a blank line.
  2. Choose Format or Minify, the keyword case (upper, lower or as written) and the indent size.
  3. Read the result. SELECT, FROM, WHERE, GROUP BY, ORDER BY and JOIN each start a line, AND and OR are lined up under the condition, and subqueries are indented inside their brackets.
  4. Copy the result or download it as a .sql file. Tick the MySQL option if your strings use backslash escapes.

Reading a formatted query

A query is easiest to read as a list of steps. The FROM line says where the rows come from. Each JOIN adds another table and the condition that links it. WHERE filters rows, GROUP BY combines them, HAVING filters the groups, and SELECT picks which columns to return. ORDER BY and LIMIT are applied at the end. The formatter puts these on separate lines in the order they appear, so you can read from top to bottom, although the database may run them in a different order.

Indentation inside brackets shows nesting. A subquery in a WHERE clause or a FROM clause is a smaller query of its own, with its own SELECT and FROM. Because the formatter indents it one level in, you can read the inner query first, understand what it returns, and then read how the outer query uses it.

Formatting habits that help teams

Agree on one style and apply it the same way every time. A shared style makes version control history cleaner, since a change to one condition shows up as a one-line difference, and it makes queries in documentation and tickets easier to compare. Putting one select column per line is a small habit with a large effect: adding or removing a column is a one-line change.

Keep the formatted version of long queries in your project, and the minified form only where size matters, such as a query stored in a configuration field. When you paste a query into a chat or ticket, format it first. Others can then see the structure at a glance, and mistakes such as a missing join condition stand out.

Tips

Frequently asked questions

What does a SQL formatter do?

It rewrites the whitespace of a query so that its structure is visible. Clauses start new lines, long lists are split one item per line, and indentation shows which parts belong together. The words and values of the query stay the same, apart from the letter case of keywords if you choose to change it.

Does it work for T-SQL, PostgreSQL and MySQL?

It handles the common core that they share, including joins, subqueries, common table expressions, CASE expressions, inserts, updates and deletes. It also reads square-bracket names from SQL Server, backtick names from MySQL and dollar-quoted strings from PostgreSQL. Features that are specific to one product are left in place on the line where they appear.

Are my strings and comments safe?

Yes. Text inside single-quoted strings, quoted names and comments is copied as written. A semicolon or the word SELECT inside a string is not mistaken for SQL, and a doubled quote inside a string is kept.

What does minify do?

It removes comments and all spaces that are not needed, leaving one line. Spaces are kept between two words, two strings and two operators that would otherwise merge, for example a minus sign followed by a negative number. A minified query returns the same results as the original.

Why does the MySQL option matter?

In standard SQL a backslash inside a string is an ordinary character, so 'C:\' is a complete string. MySQL, by default, treats a backslash as an escape, so 'it\'s' is one string. The tool needs to know which rule you use to find where each string ends.

Related: JSON formatter and validator, Regex tester, Case converter.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Free Tools

We.Inc is an AI-powered website builder you can resell under your own brand. Launch a branded client dashboard, bill on Stripe Connect, and deliver AI-generated websites in minutes. White-label plans start at $99 a month for 25 client sites, with a 7-day free trial and no per-site fees.

Product

Who It's For

Features

Resources

Company

View Sitemap