Skip to main content

SQL Formatter

A tool to format SQL queries for readability and compress them. Clean up complex queries and unify SQL coding style within your team!

Examples:
Formatted SQL will appear here...

💡 Tips

  • 복잡한 쿼리를 읽기 쉽게 포맷팅하세요
  • 팀 내 SQL 코딩 스타일을 통일하세요
  • Compress 기능으로 쿼리를 한 줄로 만들 수 있습니다
  • 여러 SQL 문을 한 번에 포맷팅할 수 있습니다

Key Features

1. SQL Formatting

  • Proper indentation for keywords, columns, and table names
  • Line breaks for improved readability
  • Consistent coding style application

2. Multiple SQL Dialect Support

  • Standard SQL
  • MySQL
  • PostgreSQL
  • MariaDB
  • PL/SQL (Oracle)
  • T-SQL (SQL Server)
  • SQLite

3. Customization Options

  • Indentation style (2 spaces, 4 spaces, Tab)
  • Keyword capitalization
  • Line spacing adjustment

4. Compression Feature

  • Remove unnecessary whitespace
  • Compress to single line

Usage Examples

Before Formatting

SELECT u.id,u.name,u.email,o.order_id,o.total FROM users u LEFT JOIN orders o ON u.id=o.user_id WHERE u.active=1 AND o.total>100 ORDER BY o.total DESC LIMIT 10;

After Formatting

SELECT
u.id,
u.name,
u.email,
o.order_id,
o.total
FROM
users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE
u.active = 1
AND o.total > 100
ORDER BY
o.total DESC
LIMIT
10;

Use Cases

1. Code Review

-- Format complex queries to make them easier to review
WITH monthly_sales AS (
SELECT
DATE_TRUNC('month', order_date) AS month,
SUM(total) AS revenue
FROM
orders
GROUP BY
DATE_TRUNC('month', order_date)
)
SELECT
month,
revenue,
LAG(revenue) OVER (
ORDER BY
month
) AS prev_month_revenue
FROM
monthly_sales;

2. Documentation

-- Clean up SQL for README or documentation
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_users_email ON users (email);

3. Debugging

-- Make single-line queries from logs readable
SELECT
p.id,
p.name,
c.name AS category,
COUNT(o.id) AS order_count
FROM
products p
INNER JOIN categories c ON p.category_id = c.id
LEFT JOIN order_items o ON p.id = o.product_id
WHERE
p.active = TRUE
GROUP BY
p.id,
p.name,
c.name
HAVING
COUNT(o.id) > 10;

4. Complex CTE (Common Table Expression)

WITH RECURSIVE subordinates AS (
SELECT
employee_id,
manager_id,
full_name,
1 AS level
FROM
employees
WHERE
manager_id IS NULL
UNION ALL
SELECT
e.employee_id,
e.manager_id,
e.full_name,
s.level + 1
FROM
employees e
INNER JOIN subordinates s ON s.employee_id = e.manager_id
)
SELECT
employee_id,
full_name,
level
FROM
subordinates
ORDER BY
level,
full_name;

Using Compression Feature

When to Use?

  1. Log Analysis: Convert multi-line queries to single line for grep search
  2. URL Parameters: When including queries in URLs
  3. Space Saving: Use simple queries inline in code

Example

-- Before compression (multi-line)
SELECT
id,
name
FROM
users
WHERE
active = TRUE;

-- After compression (single line)
SELECT id,name FROM users WHERE active=TRUE;

Best Practices

1. Consistent Style

Use the same format settings within the team:

  • Indentation: 2 spaces
  • Keywords: UPPERCASE
  • Line spacing: 1

2. Use CTE for Complex Queries

-- Good: Separated with CTE
WITH active_users AS (
SELECT * FROM users WHERE active = TRUE
),
recent_orders AS (
SELECT * FROM orders WHERE created_at > NOW() - INTERVAL '30 days'
)
SELECT
u.*,
COUNT(o.id) AS order_count
FROM
active_users u
LEFT JOIN recent_orders o ON u.id = o.user_id
GROUP BY
u.id;

3. JOIN Instead of Subquery

-- Bad: Subquery
SELECT
*
FROM
orders
WHERE
user_id IN (
SELECT
id
FROM
users
WHERE
active = TRUE
);

-- Good: Using JOIN
SELECT
o.*
FROM
orders o
INNER JOIN users u ON o.user_id = u.id
WHERE
u.active = TRUE;

4. Explicit Column Listing

-- Bad: SELECT *
SELECT * FROM users;

-- Good: Explicit columns
SELECT
id,
name,
email,
created_at
FROM
users;

Supported SQL Syntax

DDL (Data Definition Language)

CREATE TABLE, ALTER TABLE, DROP TABLE
CREATE INDEX, DROP INDEX
CREATE VIEW, DROP VIEW

DML (Data Manipulation Language)

SELECT, INSERT, UPDATE, DELETE
MERGE (some dialects)

Advanced Features

-- CTE (Common Table Expression)
WITH cte_name AS (...)

-- Window Functions
ROW_NUMBER(), RANK(), LAG(), LEAD()

-- Joins
INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, CROSS JOIN

-- Subqueries
WHERE id IN (SELECT ...), FROM (SELECT ...) AS subquery

Tips and Tricks

1. Write Long Queries Step by Step

-- Step 1: Basic SELECT
SELECT * FROM users;

-- Step 2: Add JOIN
SELECT * FROM users u
JOIN orders o ON u.id = o.user_id;

-- Step 3: WHERE condition
SELECT * FROM users u
JOIN orders o ON u.id = o.user_id
WHERE u.active = TRUE;

-- Step 4: Format

2. Use Comments

-- Query user order statistics
SELECT
u.id,
u.name,
COUNT(o.id) AS total_orders, -- Total orders
SUM(o.total) AS total_revenue -- Total revenue
FROM
users u
LEFT JOIN orders o ON u.id = o.user_id
GROUP BY
u.id,
u.name;

3. Consistent Table Aliases

-- Use first letter
FROM users u, orders o

-- Use meaningful abbreviation
FROM users usr, orders ord

Important Notes

  • Formatter does not validate SQL syntax
  • There may be slight differences between dialects
  • Compression feature sacrifices readability (use only when needed)
  • Comments are preserved during formatting

Keyboard Shortcuts

Many SQL editors provide formatting shortcuts:

  • VS Code: Shift + Alt + F
  • JetBrains: Ctrl + Alt + L
  • DBeaver: Ctrl + Shift + F