SQL Formatter
FREE TOOL · NO SIGN-UP
This SQL formatter turns long, single-line or inconsistently written queries into clean, indented SQL. Paste a query and it instantly puts each major clause on its own line, lists columns one per line and applies the keyword case you choose. It’s handy for developers, analysts and SEOs pulling data from a database who want queries that are easy to read, review and debug.
What a SQL Formatter Does
A SQL formatter, sometimes called a SQL beautifier or pretty printer, changes the layout of a query without changing its logic. The database runs the formatted version exactly the same way. The difference is that a person can now read it.
Indentation and line breaks
The formatter starts a new line for each main clause: SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING, LIMIT, each kind of JOIN, plus INSERT, UPDATE, SET, VALUES, WITH and UNION. Conditions joined with AND or OR, and ON clauses in joins, are indented one level under their parent so you can see the filter logic at a glance.
Selected columns go one per line, each followed by a comma. Opening a parenthesis, for a subquery or function call, increases the indent, and closing it steps back out. Comments and quoted strings are kept exactly as written, so values like 'O''Brien' or a -- note won’t be altered.
Keyword casing
SQL keywords aren’t case sensitive, so select, SELECT and Select all work. Consistent casing still helps readability. The formatter can change recognised keywords to UPPERCASE, lowercase, or keep them as is. Table names, column names and string values are never changed, only keywords like SELECT, JOIN, COUNT or DISTINCT.
How to Format SQL
The tool formats live, so there’s no button to press.
Step 1 – Paste the query
Paste your SQL into the SQL box. It can be a single SELECT, a long report query, an INSERT or several statements separated by semicolons. Each semicolon ends a statement and starts the next one on a fresh line.
Step 2 – Check your syntax style
There’s no dialect switch to set. The formatter works on the shared structure of SQL, so standard queries written for MySQL, PostgreSQL, SQL Server or SQLite format cleanly. If you use vendor-specific syntax, skim the output once to make sure it reads the way you expect.
Step 3 – Set case and indent
Choose Keyword case and an Indent of 2 or 4 spaces. Match your team’s style guide if you have one, so formatted queries don’t create noisy changes in version control.
Step 4 – Copy formatted SQL
Click Copy to grab the result, or Download to save it as query.sql. The panel also shows input and output character counts. Clear resets the box.
SQL Style Conventions
There’s no single official SQL style. What matters most is that everyone on a project formats queries the same way.
Leading vs trailing commas
With trailing commas, each column ends with a comma: id, then name, then email. This is the most common style and the one this tool produces. With leading commas, the comma starts the next line: , name. Fans of leading commas like that you can comment out the last column without breaking the query, and diffs stay cleaner when columns are added. Either is fine; pick one and stick with it.
Aliasing and naming
Give tables short but meaningful aliases, such as o for orders or c for customers, and always use AS for column aliases so readers can tell a rename from a typo. Use consistent naming for tables and columns, typically lowercase with underscores. Avoid single-letter aliases in long queries with many joins, because a, b and c quickly become impossible to follow.
Formatting Complex Queries
JOINs, CTEs and subqueries
Complex queries benefit most from formatting. Each join starts on its own line with its ON condition indented below, so you can trace how tables connect. Subqueries inside parentheses are indented one level deeper. Common table expressions written with WITH read top to bottom like steps in a recipe, and they’re often easier to maintain than deeply nested subqueries.
Window functions
Window functions such as ROW_NUMBER() OVER (PARTITION BY ... ORDER BY ...) pack a lot into one expression. The formatter keeps function calls together but indents what’s inside the parentheses, so the partition and order clauses are easier to spot. If an OVER clause is very long, give it its own line in your final version.
Formatting SQL Across Dialects
MySQL vs PostgreSQL syntax
Most everyday SQL is the same in MySQL and PostgreSQL. Differences show up in details: MySQL quotes identifiers with backticks, PostgreSQL uses double quotes, and PostgreSQL adds features like :: casts and RETURNING. The formatter recognises backtick and double-quoted identifiers, :: casts and RETURNING, so these format without errors.
T-SQL quirks
SQL Server’s T-SQL uses square brackets for identifiers, TOP instead of LIMIT, and procedural statements like DECLARE and BEGIN ... END. Standard SELECT queries format well, but long stored procedures with control flow are better handled by your database IDE’s built-in formatter.
SQL Formatting Mistakes
Pasting production data
Queries can include customer emails, IDs or other sensitive values in WHERE clauses or INSERT statements. This formatter runs in your browser and doesn’t upload anything, but replace real values with placeholders before sharing formatted SQL with anyone.
Wrong dialect parsing
Vendor-specific syntax can come out formatted oddly. Formatting never changes how SQL runs, but always test the query rather than assuming it’s valid because it looks tidy.
Formatting dynamic SQL strings
SQL built inside application code as strings may contain placeholders or concatenation. Format the finished query, not the code that builds it.
Write Queries Your Team Can Read
A tidy query is easier to trust, review and fix six months later. Use the SQL formatter whenever you inherit a messy query or write a complex one, then shrink it for embedding with the SQL Minifier. If you work with API data, the JSON Formatter & Minifier and XML Formatter & Minifier do the same job for those formats. Find more free developer and SEO tools in the FHSEO Hub free SEO tools collection.
