← All Articles ยท ยท 2 min read

SQL Formatter: Clean Up Messy SQL Queries in Seconds

How to format SQL queries instantly. Covers indentation rules, keyword capitalization, JOIN formatting, subqueries, and the best SQL formatters for PostgreSQL, MySQL, and SQLite.

sqlformatterdatabasepostgresqlmysqlquery

SQL Formatter: Clean Up Messy SQL Queries in Seconds

Why SQL Formatting Matters

Unformatted SQL is unreadable:

-- โŒ Impossible to parse visually
SELECT u.id,u.name,u.email,o.id,o.total,o.created_at FROM users u JOIN orders o ON u.id=o.user_id WHERE o.total>100 AND u.created_at>'2026-01-01' ORDER BY o.total DESC LIMIT 20;
-- โœ… Instantly readable
SELECT
  u.id,
  u.name,
  u.email,
  o.id,
  o.total,
  o.created_at
FROM users u
  JOIN orders o ON u.id = o.user_id
WHERE
  o.total > 100
  AND u.created_at > '2026-01-01'
ORDER BY o.total DESC
LIMIT 20;

Standard SQL Formatting Rules

ElementRuleExample
KeywordsUPPERCASESELECT, FROM, WHERE
Table namesas-isusers, orders
AliasesShort, lowercaseu, o, ua
ComparisonSpaces around =total > 100
CommasLeading (no trailing)name, not name ,
Indentation2 or 4 spacesNested clauses indented

Formatting JOINs Correctly

-- โŒ JOINs on same line as FROM
FROM users u JOIN orders o ON u.id=o.user_id

-- โœ… JOINs on separate lines
FROM users u
  JOIN orders o ON u.id = o.user_id
  LEFT JOIN addresses a ON u.id = a.user_id

-- โœ… Multiple conditions
WHERE
  o.total > 100
  AND o.status = 'completed'
  AND a.country IN ('TW', 'HK', 'CN')

Subqueries

SELECT
  u.name,
  (
    SELECT COUNT(*)
    FROM orders o
    WHERE o.user_id = u.id
  ) AS order_count
FROM users u
WHERE
  EXISTS (
    SELECT 1
    FROM orders o
    WHERE o.user_id = u.id
      AND o.total > 500
  )
ORDER BY order_count DESC;

Common Mistakes

MistakeCorrection
select * fromSELECT * FROM (uppercase keywords)
WHERE id=1WHERE id = 1 (spaces around =)
name ,emailname, email (comma before value)
SELECT id, name, FROM usersRemove trailing comma before FROM
WHERE status='active AND total>100WHERE status = 'active' AND total > 100

Dialect Differences

DialectExtra Keywords
PostgreSQLRETURNING, ON CONFLICT, WITH ORDINALITY
MySQLREGEXP, IGNORE, HIGH_PRIORITY
SQLiteRETURNING (v3.35+), ON CONFLICT
T-SQL (SQL Server)TOP, WITH (nolock)

Online SQL Formatters

Use a formatter that supports dialect selection โ€” formatting rules differ slightly between databases.

Free Newsletter

Level Up Your Dev Workflow

Get new tools, guides, and productivity tips delivered to your inbox.

Plus: grab the free Developer Productivity Checklist when you subscribe.

Found this guide useful? Check out our free developer tools.