Code Tools

SQL Formatter

All tools

Split a long single-line query at its keywords so SELECT, FROM, JOIN and WHERE are easy to scan.

How it works

Four simple steps

1

Paste the query

Drop in a logged or hand-written SQL statement of any length.

2

Keywords split out

Each known keyword is uppercased and moved to a new line.

3

Scan the conditions

AND and OR lines are indented under the WHERE clause.

4

Copy or save

Copy the result or download it as query.sql.

SQL Formatter

About SQL Formatter

An ORM logs the query it generated as a single 900-character line and you need to see which join is producing duplicate rows. Breaking the statement apart at its keywords is usually enough to spot the problem.

The formatter first collapses all whitespace, then starts a new line before each recognized SQL keyword and converts it to uppercase. The list covers clauses (SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING, LIMIT, OFFSET), join keywords, INSERT, UPDATE, DELETE, CREATE, ALTER and DROP, plus words like AS, IN, NOT, NULL, LIKE, BETWEEN, CASE, WHEN, THEN, ELSE, END, ASC and DESC. Lines that begin with AND or OR are indented two spaces under their WHERE. Output updates as you type, and Download saves query.sql.

The keyword rule is applied everywhere, which makes it aggressive. LEFT and JOIN land on separate lines, IS NOT NULL is split in three, and a keyword inside a quoted string (say 'Order by phone') is also broken out and uppercased. Use the result for reading and code review rather than pasting it straight into production code. If you are writing a fresh statement, the visual SQL query builder is a better starting point, and spreadsheet rows can become INSERT statements with CSV to SQL. Browse the other code utilities for related formatters.

Queries are rewritten with a few regular expressions in your browser, so table names and literal values copied from production logs are not sent to a server.

Keyboard shortcut

Ctrl Enter - Run tool
FAQ

Frequently asked questions

See all FAQs

Answers for this tool. For site-wide help, open the FAQ hub.

It does not parse any dialect. It matches a fixed list of common keywords, so SELECT, INSERT, UPDATE and DDL statements from MySQL, PostgreSQL, SQL Server or SQLite all get the same treatment. Dialect-specific words such as RETURNING or QUALIFY stay inline.

LEFT, RIGHT, INNER, OUTER and JOIN are separate entries in the keyword list, and each one gets its own line break. Join them back by hand if you paste the result into source code.

It can. Whitespace inside quotes is collapsed, and keywords that appear inside strings are uppercased and moved to a new line. Compare literals against the original before running the formatted query.

View more FAQs

Related tools

View all

HTML Formatter

Beautify and indent HTML markup.

Open

CSS Formatter

Format and pretty-print CSS.

Open

JS Formatter

Beautify JavaScript code.

Open

JSON Formatter

Beautify and validate JSON.

Open

Explore 321 free tools

Everything you need as a developer, marketer or creator - in one beautiful place.

Browse all tools