SQL Formatter
Format, minify, or uppercase SQL queries.
SQL Formatter — What It Does
Paste any SQL query — however messy — and instantly produce a clean, consistently indented, readable version. Options include pretty-print formatting with one clause per line, keyword uppercasing, and minification (all whitespace removed). Works with SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, CTEs, subqueries, and JOINs. Runs entirely in your browser.
SQL Formatting Conventions
- Major clauses (
SELECT,FROM,WHERE,JOIN) start on new lines - Column lists indented under
SELECTfor multi-column queries - Keywords in UPPERCASE, identifiers in lowercase
- Each
AND/ORcondition on its own line underWHERE - Subqueries indented relative to their parent clause
Before and After Example
Unformatted:
select u.id,u.name,o.total from users u join orders o on u.id=o.user_id where o.total>100 order by o.total desc limit 10
Formatted:
SELECT u.id, u.name, o.total FROM users u JOIN orders o ON u.id = o.user_id WHERE o.total > 100 ORDER BY o.total DESC LIMIT 10
SQL Formatting Tips
- Format before code review — Consistent formatting removes style discussions and lets reviewers focus on logic.
- Use aliases consistently — Short, meaningful table aliases (e.g.,
ufor users) improve readability significantly. - Minify for ORM storage — Some ORMs or config files work better with single-line SQL strings.
- Comment complex logic — After formatting, add
-- commentsto explain non-obvious JOINs or filter conditions.
Frequently Asked Questions
- Why should I format my SQL queries?
- Formatted SQL is dramatically easier to read, debug, and review. Each major clause (SELECT, FROM, WHERE, JOIN, GROUP BY) on its own line makes query structure immediately visible. Code reviewers can diff formatted SQL effectively, and you can spot logic errors at a glance.
- What SQL dialects does the formatter support?
- The formatter handles standard ANSI SQL constructs as well as common extensions used by MySQL, PostgreSQL, SQL Server (T-SQL), and SQLite. This includes JOIN types, subqueries, CTEs (WITH clauses), window functions, and dialect-specific functions like ISNULL or COALESCE.
- What is SQL minification and when is it useful?
- SQL minification removes all unnecessary whitespace and newlines, producing a compact single-line query. This is useful when embedding SQL in configuration files, URLs, or code that has line-length limits — or when you want to measure character count. It does not affect query execution.
- Should SQL keywords be uppercase or lowercase?
- SQL keywords (SELECT, WHERE, FROM, JOIN, etc.) are case-insensitive in most databases, but the convention in professional codebases is to write keywords in UPPERCASE and identifiers (table/column names) in lowercase or snake_case. This makes keywords visually distinct from identifiers.
- Does formatting SQL change how it executes?
- No. Whitespace (spaces, tabs, newlines) is ignored by SQL parsers. A single-line query and a multi-line formatted version of the same query produce identical execution plans. Formatting is purely for human readability and has no impact on query performance.