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
- SQL
- Output
- Keyword case
- Indent
- Strings use backslash escapes (MySQL style)
What you get
- Lines
- Characters
How it works
- Paste one or more statements into the box. Statements ending in a semicolon are separated by a blank line.
- Choose Format or Minify, the keyword case (upper, lower or as written) and the indent size.
- 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.
- 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
- Formatting never changes names. Table and column names keep their exact spelling, because in some databases the case of a name matters.
- Keyword case is a style choice. Upper-case keywords make the structure of a long query easy to scan; lower case reads more like ordinary code.
- If a query is slow, formatting it first shows the shape: how many joins, which tables are scanned and where the filters are. That is the starting point for tuning.
- Comments with two hyphens run to the end of the line, so minifying removes them. The tool keeps hint comments that start with /*+ or /*!, since some databases use them as instructions.
- The tool does not check that the SQL is valid. A formatted query with a typo still has the typo, so run it in your database to find syntax errors.
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
- Srcset Generator
- Business Startup Costs Calculator
- Store Hours Sign Maker: Print It, Then Put It on Your Site
- SVG Optimizer
- SVG Background Pattern Generator
- SVG to PNG Converter for Website Logos
- SVG Viewer and Editor
- SVG Wave and Shape Divider Generator
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App