How to Format SQL Queries

March 22, 2026 · 4 min read

Unformatted SQL is hard to read, hard to debug, and hard to maintain. Whether you're pulling a query from a log file, inheriting someone else's code, or building a complex join, proper formatting makes a huge difference.

This guide covers the key principles of SQL formatting and shows you how to clean up any query — manually or with a free SQL formatter.

Why Format SQL?

Consider this query:

SELECT u.id, u.name, u.email, o.id as order_id, o.total, o.created_at FROM users u INNER JOIN orders o ON u.id = o.user_id WHERE o.total > 100 AND u.created_at > '2025-01-01' ORDER BY o.total DESC LIMIT 50;

It works, but good luck finding a bug in it. Now compare the formatted version:

SELECT u.id, u.name, u.email, o.id AS order_id, o.total, o.created_at FROM users u INNER JOIN orders o ON u.id = o.user_id WHERE o.total > 100 AND u.created_at > '2025-01-01' ORDER BY o.total DESC LIMIT 50;

Same query, but now you can instantly see which columns are selected, which tables are joined, and what the conditions are.

SQL Formatting Rules

1. Major Keywords on New Lines

Each major SQL clause should start on a new line:

2. Indent Column Lists and Conditions

Columns in SELECT, conditions in WHERE, and join conditions should be indented under their keyword:

SELECT first_name, last_name, email FROM users WHERE active = true AND role = 'admin'

3. Uppercase Keywords

A common convention is to write SQL keywords in UPPERCASE and table/column names in lowercase:

SELECT name FROM users WHERE id = 1; -- preferred select name from users where id = 1; -- works but harder to scan

4. One Column Per Line in SELECT

When selecting multiple columns, put each on its own line. This makes diffs cleaner and makes it easy to comment out individual columns during debugging.

5. Align JOINs Consistently

FROM orders o INNER JOIN users u ON o.user_id = u.id LEFT JOIN products p ON o.product_id = p.id

6. Use Subquery Indentation

SELECT * FROM users WHERE id IN ( SELECT user_id FROM orders WHERE total > 1000 )

Format SQL Instantly

Don't want to format by hand? Paste your SQL into the UtilShed SQL Formatter and get clean, readable output in one click.

Format your SQL query

Paste messy SQL, get properly indented and formatted output.

Open SQL Formatter

SQL Formatting in Your Editor

VS Code

Install the SQL Formatter extension (by adpyke). Right-click any SQL file and select "Format Document" or use Shift+Alt+F.

JetBrains (IntelliJ, DataGrip)

Built-in: Ctrl+Alt+L (or Cmd+Alt+L on Mac) formats SQL in any .sql file or SQL console.

Command Line

# Using sql-formatter (Node.js) npm install -g sql-formatter sql-formatter < messy.sql > clean.sql # Using sqlformat (Python) pip install sqlparse sqlformat --reindent --keywords upper messy.sql > clean.sql

SQL Style Guides

If your team doesn't have a SQL style guide, consider adopting one of these:

The most important rule: pick a style and be consistent. Formatted SQL is easier to read, review, and debug.

Related Tools