How to format SQL
Paste a query, pick the dialect that matches your database, and the formatter puts each clause on its own line, indents subqueries and uppercases keywords. Formatting only changes whitespace and keyword case, never what the query does, so it's safe to run on queries you're about to execute.
Before and after
select u.id,u.name,count(o.id) as orders from users u left join orders o on o.user_id=u.id where u.active=1 group by u.id,u.name having count(o.id)>5 order by orders desc SELECT u.id, u.name, count(o.id) AS orders FROM users u LEFT JOIN orders o ON o.user_id = u.id WHERE u.active = 1 GROUP BY u.id, u.name HAVING count(o.id) > 5 ORDER BY orders DESC
Why the dialect matters
Databases differ in quoting and syntax: MySQL quotes names with backticks, PostgreSQL and standard SQL use double quotes, and features like :: casts or RETURNING exist only in some dialects. Choosing the right dialect lets the formatter recognise these instead of splitting them in odd places.
Conventions that make queries easier to review
- One selected column per line, so a diff shows exactly which column changed.
- Each JOIN on its own line with its ON condition, so missing join conditions stand out.
- Use CTEs (
WITH name AS (...)) instead of deeply nested subqueries for multi-step logic. - Name every aggregate with AS, and prefer explicit column lists over
SELECT *in application code.
Related tools
To turn a list of values into an IN (...) clause, use the SQL IN Generator. To write a query from a plain-English description, try the AI Text-to-SQL Generator, and check its output before running it.