SQL Formatter: Beautify SQL Queries Online
Use this sql formatter to turn long, messy queries into clean, readable SQL. Paste your query, pick a dialect such as MySQL, PostgreSQL or SQL Server, and it lines up keywords, joins and clauses. Choose upper or lower case keywords and copy the result.
SQL Formatter
Beautify or minify SQL.
How to use the SQL Formatter
- Paste your SQL query.
- Choose the dialect and keyword case.
- Press Format, or Minify for one line.
- Copy the result.
How it works
Formatting uses the open-source sql-formatter library, which tokenizes the query for the chosen dialect and puts each clause (SELECT, FROM, JOIN, WHERE, ORDER BY) on its own line with indented columns and conditions. Minify removes comments and extra whitespace outside strings.
Example
"select id,name from users where active=1" becomes SELECT, then id and name indented on their own lines, then FROM users and WHERE active = 1.
Frequently asked questions
Does formatting change what the query does?
No, only whitespace and keyword case change.
Is my SQL sent to a server?
No. It is formatted in your browser.
Which dialect should I pick?
The database you use; Standard SQL works for most queries.
See also: JSON Formatter & Validator, HTML Formatter and Minifier.
Format a SQL query
How to use it
- Paste your SQL.
- Pick a dialect.
- Choose keyword case and indent.
- Copy the formatted query.
More questions
Can it minify SQL?
Yes. Compact your SQL onto one line to save space.
Related tools: JSON Formatter & Validator, Online Spreadsheet, XML Formatter and Validator.
One query, formatted and minified
Paste your query, choose the dialect, the keyword case and 2 or 4 spaces, then press Format. Press Minify for a single line with comments removed. To turn a CSV export into JSON for an app, use the CSV to JSON Converter.
Worked example: a 182-character query that joins users to orders formats to 17 lines with the keywords in uppercase (SELECT, FROM, LEFT JOIN, WHERE, GROUP BY, ORDER BY, LIMIT), each column on its own line. Minify turns the same query into one line of 174 characters. Choosing Keep as typed leaves the lowercase keywords alone.
More answers
Does Minify remove comments?
Yes. Both -- line comments and /* block */ comments are removed, and the whitespace outside quotes is collapsed to single spaces. Spaces around commas, semicolons, parentheses and equals signs are dropped too.
What does the Keep as typed option do?
It leaves keyword capitalization exactly as you typed it, so a lowercase query stays lowercase while the tool still puts each clause on its own line. UPPERCASE and lowercase options change every keyword.