How to Format SQL Queries — Online SQL Formatter Guide
Unformatted SQL is hard to read, harder to debug, and a nightmare to maintain. Properly formatted SQL makes complex queries understandable at a glance, simplifies code reviews, and reduces errors. This guide covers SQL formatting best practices and shows you how to format queries instantly with an online tool.
Why Format SQL?
Consider a real-world query with joins, subqueries, and aggregates. Without formatting, it becomes a wall of text that's nearly impossible to parse visually. Formatting adds structure that mirrors the logical hierarchy of the query.
Before Formatting
SELECT u.id, u.name, COUNT(o.id) as order_count FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE u.created_at > '2024-01-01' GROUP BY u.id, u.name HAVING COUNT(o.id) > 5 ORDER BY order_count DESC
After Formatting
SELECT u.id, u.name, COUNT(o.id) AS order_count FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE u.created_at > '2024-01-01' GROUP BY u.id, u.name HAVING COUNT(o.id) > 5 ORDER BY order_count DESC
Core Formatting Rules
Follow these conventions for consistent, readable SQL:
- Uppercase keywords: Write
SELECT,FROM,WHERE,JOIN, etc. in uppercase to distinguish them from table and column names. - One clause per line: Place each major clause (SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY) on its own line.
- Indent subqueries: Nested queries should be indented to show the hierarchy.
- Align commas: Place commas at the end of the line, not the beginning, for easier reordering.
- Use aliases: Short table aliases (e.g.,
uforusers) improve readability in joins.
Common Patterns
Here are formatted examples of frequently used SQL patterns:
INSERT with multiple rows:
INSERT INTO products (name, price, category) VALUES ('Widget A', 29.99, 'widgets'), ('Gadget B', 49.99, 'gadgets'), ('Tool C', 19.99, 'tools');
CTE (Common Table Expression):
WITH monthly_sales AS ( SELECT DATE_TRUNC('month', created_at) AS month, SUM(amount) AS total FROM orders GROUP BY 1 ) SELECT month, total, LAG(total) OVER (ORDER BY month) AS prev_month FROM monthly_sales ORDER BY month;
Formatting Complex Queries
For queries with multiple joins and subqueries, consistent formatting becomes essential. Break long conditions across multiple lines and align related logic vertically:
SELECT o.id, o.total, u.name AS customer_name, COUNT(oi.id) AS item_count FROM orders o INNER JOIN users u ON o.user_id = u.id LEFT JOIN order_items oi ON o.id = oi.order_id WHERE o.status = 'completed' AND o.created_at >= DATE '2024-01-01' AND o.total > 100 GROUP BY o.id, o.total, u.name HAVING COUNT(oi.id) > 3 ORDER BY o.total DESC LIMIT 50;
Auto-Formatting Tools
While manual formatting is good practice, auto-formatters save time and enforce consistency. Most modern IDEs have built-in formatters or plugins. Online formatters are ideal for quick formatting without installing anything.
Popular options include SQLFluff (CLI), pgFormat (PostgreSQL-specific), and online tools like ToolStack's SQL Formatter that work instantly in your browser.
Try It Online
Format Your SQL Queries Instantly
Paste unformatted SQL into ToolStack's free formatter and get clean, readable queries in seconds. Works with PostgreSQL, MySQL, and SQLite syntax.