SQL formatter
Format SQL queries with each clause on its own line, consistent indentation and the keyword case you prefer. Choose your database’s dialect (PostgreSQL, MySQL, SQL Server, SQLite, Oracle and others) so its syntax is understood.
Worked example
A reporting query on one line, formatted as standard SQL with upper-case keywords.
select c.name,sum(o.total) as revenue from customers c join orders o on o.customer_id=c.id where o.status='paid' group by c.name order by revenue desc
SELECT c.name, sum(o.total) AS revenue FROM customers c JOIN orders o ON o.customer_id = c.id WHERE o.status = 'paid' GROUP BY c.name ORDER BY revenue DESC
Common mistakes
Using the wrong dialect
If a valid query is rejected or comes out oddly split, choose your database in the dialect list. Placeholders such as $1, ? and :name also depend on it.
Treating formatting as validation
A neatly formatted query can still reference a missing column. Run it against your database to be sure.
Pasting a query still wrapped in code
"SELECT * " + "FROM users" is a string in your program, not SQL. Remove the quotes and the + signs first.
Changing the case of quoted names
In PostgreSQL "UserId" and userid are different names. The formatter never touches quoted names; don’t retype them in another case by hand.
How it works
- 1
Paste a query
One statement or several separated by semicolons, such as a query copied from a log or an ORM.
- 2
Pick dialect and keyword case
Standard SQL covers common queries. Keywords can be UPPER, lower or left as written.
- 3
Copy the query
Each statement is formatted, with a blank line between statements, ready for a .sql file or a code review.
Questions
- Which dialect should I choose?
- The one your database uses. Standard SQL handles everyday SELECT, INSERT, UPDATE and DELETE, but features such as PostgreSQL’s :: casts and ON CONFLICT, or SQL Server’s [bracketed] names, are only recognized with their dialect selected.
- Does it check that my query is valid?
- No. It follows keywords, brackets and commas to lay the query out; it doesn’t know your tables or columns. It reports an error only when it can’t follow the structure, such as an unclosed bracket. A misspelled keyword passes through unchanged.
- Does keyword case change my names or strings?
- No. Only keywords and data types change case. Table and column names, function names, quoted identifiers and string values stay exactly as written.
- Which formatter is used?
- The open-source sql-formatter library, running in this page. Your queries are never sent anywhere.