Skip to main content

SQL Formatter — make long queries readable

Formats SQL queries in the clause-per-line style, with keywords in upper case and joins, conditions and subqueries indented. Choose 2 spaces, 4 spaces or a tab; everything runs in your browser.

Features

  • Formats SQL with one clause per line
  • Upper-cases SQL keywords, keeps your table and column names
  • Indents subqueries, JOINs and AND/OR conditions
  • Indentation with 2 spaces, 4 spaces or tabs
  • Minify mode puts a query on a single line
  • Runs entirely in your browser, nothing is uploaded

When a SQL Formatter Helps

SQL rarely arrives in a readable shape. Queries copied from application logs, ORM debug output or a slow-query report usually sit on one long line, and hand-written queries drift apart in style from one developer to the next. With one clause per line you see at a glance which tables are joined, which conditions filter the rows and where a subquery begins and ends.

This page opens the Code Formatter with SQL and beautify mode already selected. Paste a query, choose 2 spaces, 4 spaces or a tab, then copy the result or download it as a .sql file. Everything happens in your browser, so queries containing table names or real values never leave your machine.

How Your Query Is Laid Out

  • Clauses such as SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, INSERT INTO, VALUES, UPDATE, SET, WITH and UNION start a new line, with their content indented one level below.
  • Joins (JOIN, LEFT JOIN, INNER JOIN and the other variants) get their own line inside the FROM block; the ON condition stays on that line.
  • AND / OR at the top level of a condition start a new line. Inside parentheses, function calls and IN lists everything stays on one line.
  • Commas at the top level put each column or value on its own line.
  • Subqueries: a SELECT or WITH inside parentheses is indented one block deeper and closed on its own line.
  • Keywords are written in upper case; table names, column names and aliases keep your spelling.
  • Strings and comments stay untouched. A -- comment ends the line, a /* … */ comment stays where it was.
  • Several statements separated by ; are formatted one after the other, with a blank line between them.

Example

A typical one-line query from a log file:

select u.id, u.name, count(o.id) as orders from users u left join orders o on o.user_id = u.id where u.active = 1 and u.name like '%a%' group by u.id, u.name order by orders desc limit 10

With 4 spaces of indentation the formatter returns:

SELECT
    u.id,
    u.name,
    COUNT(o.id) AS orders
FROM
    users u
    LEFT JOIN orders o ON o.user_id = u.id
WHERE
    u.active = 1
    AND u.name LIKE '%a%'
GROUP BY
    u.id,
    u.name
ORDER BY
    orders DESC
LIMIT
    10

Limits and Tips

  • Not a validator: the formatter only changes whitespace and the case of keywords. It does not check your syntax, so a typo such as selec passes through unchanged without an error. Test the query in your database.
  • Not dialect-aware: it works from a fixed keyword list, not a parser for MySQL, PostgreSQL or SQL Server. Column names that are also keywords, such as date or key, are upper-cased as well, and in BETWEEN 1 AND 5 the AND moves to a new line.
  • Operator spacing: operators get a space on each side, so a=1 becomes a = 1. Check unusual forms: t.* comes out as t. *.
  • Minify: removes -- and /* */ comments and collapses whitespace to single spaces. Text inside quotes stays exactly as written, comment markers and repeated spaces included. Keywords keep the case you typed.

Useful Resources

Wikipedia: SQL Overview of the SQL language, its history and the ISO standard
Wikipedia: Join (SQL) Inner, outer and cross joins explained with example tables
SQL Style Guide by Simon Holywell Widely used conventions for naming, keyword case and layout of SQL
PostgreSQL: SELECT Complete syntax of SELECT, including WITH, JOIN, GROUP BY and LIMIT
SQLite: SELECT Syntax diagrams for the SELECT statement in SQLite