SQL Formatter

Format SQL text for better readability.

Before

After

What Is an SQL Formatter?

A long SQL statement crammed onto one line gives up nothing about its structure. This tool inserts line breaks around keywords such as SELECT, FROM, WHERE and JOIN, and indents nested subqueries, so the shape of the statement becomes something your eye can follow. Formatting never changes the meaning.

The dialect you choose affects the result. MySQL, PostgreSQL, Oracle and SQL Server each have their own syntax, so pick the one matching the database you use. Note also that SQL containing a syntax error cannot be parsed correctly and may come back unformatted — in that case, check the validity of the SQL itself first.

Steps to Format SQL

  1. Paste your SQL Paste the SQL you want formatted into the input box. Separate statements with semicolons and several can be processed together.
  2. Choose the dialect Select the one matching the database you are using. If you are unsure, standard SQL will still format the great majority of syntax.
  3. Set indentation and keyword casing Adjust the indent width to your team's convention. Turning on upper-casing lines the keywords up and makes the statement easier to read.
  4. Format and check the result Press «Format» and the result appears, ready to paste straight into a code review or a specification.

Tips for getting more out of it

  • SQL keywords such as SELECT, FROM, WHERE, and JOIN are automatically line-broken and aligned, and nested subqueries are properly indented.
  • Enabling "Capitalize keywords" auto-uppercases all keywords, improving readability.
  • Adjust the indent size to match your team's coding style guidelines.
  • Pasting the formatted SQL into code reviews or documentation makes it much easier to read.
  • Supports major SQL dialects including MySQL, PostgreSQL, and Oracle.

When an SQL Formatter Comes in Handy

Making sense of SQL you have been handed

Format a one-line statement pulled from a log or application source and the structure of the join conditions and filters becomes clear at a glance.

Tidying up before a code review

Submit it with the indentation already aligned and your reviewer can concentrate on the logic rather than the layout. The diff also reads more easily.

Putting SQL into a specification or documentation

Paste formatted SQL into a document and readers can follow the structure. Combined with upper-casing, it also reads well in print.

Analysing the shape of a slow query

Because you can see the depth of the subqueries and the number of joins, you can form a hypothesis before reading the execution plan.

SQL Terms Explained

SQL Dialect
The syntactic differences between database products. Each product adds its own extensions to standard SQL, which is why the choice matters when formatting.
Subquery
A query written nested inside another SQL statement. The deeper it goes, the harder it is to read — which is where indentation pays off most.
JOIN
The syntax for retrieving data by tying several tables together. Join conditions tend to run long, so where the line breaks fall governs readability.
Prepared Statement
An approach that builds a query with values inserted afterwards. It is the foundation of any defence against SQL injection.
SQL Injection
An attack that mixes fragments of SQL into input values to make an unintended query run. Building SQL by string concatenation invites it.
Stored Procedure
A sequence of processing kept on the database side. Because it contains control-flow syntax, it may fall outside what formatting covers.

FAQ

MySQL, PostgreSQL, Oracle (PL/SQL), and SQL Server (T-SQL) are supported alongside standard SQL. Select the matching dialect from the dropdown to format dialect-specific syntax correctly.

If the input SQL contains syntax errors, the formatter may not parse it correctly. Check that your SQL is valid using a database client before formatting.

You can paste multiple SQL statements separated by semicolons and they will be formatted together. However, stored procedures and PL/SQL block syntax may not be fully supported.
Tool-kun

Side Note — 50 Years of SQL: A History of Relational Databases

SQL originated when IBM developed "SEQUEL" based on the relational model proposed by E.F. Codd (IBM) in a 1970 paper (later renamed SQL for trademark reasons). It was standardized by ANSI in 1986 and has been used in countless databases ever since.

One of the most famous security incidents is the SQL injection attack. The 2011 Sony PSN (PlayStation Network) breach exposed over 100 million personal records, with estimated damages of $171 million. It remains a staple on the OWASP Top 10 list, and using prepared statements is considered the fundamental countermeasure.

"WHERE 1=1" is a handy technique for building dynamic SQL, but it's also a classic SQL injection example. The ability to log in without a password by entering ' OR '1'='1 into a login form is still used as a canonical example in security training.