SQL Formatter
Format, indent, and minify SQL for PostgreSQL, MySQL, and SQLite with uppercase keywords and one clause per line.
Runs entirely in your browser — nothing you paste is uploaded.
SELECT c.id, c.email, count(o.id) AS order_count, sum(o.total) AS lifetime_value FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.created_at >= '2026-01-01' AND o.status <> 'refunded' GROUP BY c.id, c.email HAVING count(o.id) > 3 ORDER BY lifetime_value DESC LIMIT 25;
One clause per line, with SELECT lists, GROUP BY, ON CONFLICT, and subqueries indented. PostgreSQL, MySQL, and SQLite keywords are all recognised.
SQL formatting cheat sheet
quick reference- SELECT … FROM … WHERE
- The clause order SQL expects. One clause per line is what makes a long query readable in a review or a log.
- LEFT JOIN … ON
- Keeps every row of the left table and fills NULL for a missing match; INNER JOIN drops unmatched rows instead.
- GROUP BY / HAVING
- HAVING filters after aggregation, while WHERE filters rows before it, so it cannot use COUNT or SUM results.
- WITH name AS (…)
- A common table expression. It is inlined by most planners but keeps a long query readable; add RECURSIVE for self-reference.
- ON CONFLICT (key) DO UPDATE
- PostgreSQL and SQLite upsert. MySQL uses INSERT … ON DUPLICATE KEY UPDATE instead.
- LIMIT n OFFSET m
- Page through results, but prefer a keyset cursor (WHERE id > last_id) once the offset grows past a few thousand rows.
Related SQL syntax guides
All SQL guidesSQL Cursors vs Set-Based Operations: Syntax, Memory, and Performance
Why a row-by-row cursor costs minutes where one UPDATE takes seconds, how server cursors spool to disk, and the set-based replacements for each loop.
SQLSQL String Functions: CONCAT, SUBSTRING, REPLACE, and Pattern Matching
The concatenation and slice functions that differ between engines, why LIKE 'x%' uses an index and '%x%' cannot, plus escaping and NULL rules.
SQLSQL Date and Time Functions: INTERVAL Arithmetic, Time Zones, and DST
timestamptz versus timestamp, half-open ranges that keep an index, and the daylight saving gaps that make local scheduling wrong once a year.
SQLSQL Row Locking: SELECT FOR UPDATE, SKIP LOCKED, and NOWAIT
How FOR UPDATE locks rows until commit, why the plan decides the lock scope, and the SKIP LOCKED claim-queue pattern that scales to many workers.