DevToolBox

SQL Formatter

SQL文を自動整形し、キーワードの大文字変換やインデントを行います。

How to Use the SQL Formatter

This free online SQL formatter helps you beautify and organize your SQL queries with proper indentation and formatting. Paste your SQL into the input field, choose your preferred indentation size, and click "Format" to instantly transform messy SQL into clean, readable code.

Features

  • Automatic keyword uppercasing (SELECT, FROM, WHERE, JOIN, etc.)
  • Proper line breaks before major SQL clauses
  • Customizable indentation (2 or 4 spaces)
  • Support for complex queries with JOINs, subqueries, and aggregations
  • Client-side processing - your queries never leave your browser

Why Format SQL?

Well-formatted SQL is easier to read, debug, and maintain. When SQL queries grow complex with multiple JOINs, WHERE conditions, and subqueries, proper formatting becomes essential for understanding the query logic. Consistent formatting also helps during code reviews and makes it easier for team members to collaborate on database queries.

SQLフォーマッターの使い方

SQL文を入力エリアに貼り付け、インデント幅とキーワード大文字化のオプションを選択して「整形」ボタンを押すと、 読みやすく整形されたSQLが出力されます。SELECT、JOIN、WHERE等の主要句ごとに改行・インデントされ、 複雑なクエリの構造を把握しやすくなります。全ての処理はブラウザ内で完結します。

Supported SQL Statements

This formatter handles all major SQL statement types including SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, and DROP. It recognizes JOIN types (INNER, LEFT, RIGHT, OUTER), aggregate functions (COUNT, SUM, AVG, MAX, MIN), and conditional expressions (CASE, WHEN, THEN). The formatter works with most SQL dialects including MySQL, PostgreSQL, SQLite, and SQL Server.

実務での活用例

1. 本番SQLをそのまま貼っても安全

本番環境のSQLにはテーブル名・カラム名・WHERE句の閾値など機密情報が含まれることが多く、 サーバー送信型のフォーマッターは避けるべきです。本ツールはブラウザ内で文字列パースを行うため、 SQLが外部に送信されることはありません。

2. よくある整形前後の差

1行でベッタリ書かれた以下のクエリ:

SELECT u.id, u.name FROM users u INNER JOIN orders o ON u.id = o.user_id WHERE o.status = 'paid' AND o.created_at >= '2026-01-01' ORDER BY o.created_at DESC LIMIT 100

を整形すると以下のように主要句ごとに改行・インデントされます:

SELECT
  u.id,
  u.name
FROM users u
INNER JOIN orders o
  ON u.id = o.user_id
WHERE o.status = 'paid'
  AND o.created_at >= '2026-01-01'
ORDER BY o.created_at DESC
LIMIT 100

3. レビュー・コードコメントでの活用

プルリクエストやIssueにSQLを貼る際、整形した形式で投稿するとレビュワーが読みやすくなります。 整形後の結果の比較はDiff Checkerで確認できます。

4. 対応方言と制約

  • MySQL / PostgreSQL / SQLite / SQL Server の一般的な構文に対応
  • ウィンドウ関数(OVER 句)や再帰CTE(WITH RECURSIVE)も整形可能
  • データベース固有の特殊構文(PostgreSQLの $$ 関数定義など)は完全対応ではないため、手動調整が必要な場合あり

よくあるエラーと対処 / Common Errors and Fixes

エラー / Error原因 / Cause対処 / Fix
column must appear in GROUP BY集約関数外の列を含めた
Non-aggregated column in SELECT
GROUP BY に追加するか集約関数で包む
Add to GROUP BY or wrap with an aggregate
ambiguous column reference複数テーブルで同名カラム
Same column name in multiple tables
テーブル別名で修飾(u.id
Qualify with table alias (u.id)
missing FROM-clause entry (PG)別名定義前に参照
Reference before alias defined
サブクエリに別名を付ける(AS t
Alias the subquery (AS t)
could not devise a query plan結合条件不足でフル直積
Missing join predicate → cartesian
ON 条件を明示
Add an explicit ON predicate
NULL との比較で常に false
NULL comparison is never true
= NULL は NULL を返す
= NULL yields NULL, not true
IS NULL / IS NOT NULL を使う
Use IS NULL / IS NOT NULL

言語別コード例 / Code Examples by Language

PostgreSQL

SELECT u.id, COUNT(*) AS orders
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE u.created_at >= NOW() - INTERVAL '30 days'
GROUP BY u.id
HAVING COUNT(*) > 0
ORDER BY orders DESC
LIMIT 100;

MySQL

SELECT u.id, COUNT(*) AS orders
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE u.created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY)
GROUP BY u.id
ORDER BY orders DESC
LIMIT 100;

BigQuery (standard SQL)

SELECT user_id, COUNT(*) AS orders
FROM `proj.ds.orders`
WHERE DATE(created_at) >= DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY)
GROUP BY user_id
ORDER BY orders DESC
LIMIT 100;

SQLite

SELECT user_id, COUNT(*) AS orders
FROM orders
WHERE created_at >= datetime('now', '-30 days')
GROUP BY user_id
ORDER BY orders DESC
LIMIT 100;

Before / After 実例 / Before & After

例1: 長い 1 行クエリを整形 / Format a single-line query

SELECT u.id,u.name FROM users u INNER JOIN orders o ON u.id=o.user_id WHERE o.status='paid' AND o.created_at>='2026-01-01' ORDER BY o.created_at DESC LIMIT 100

SELECT
  u.id,
  u.name
FROM users u
INNER JOIN orders o
  ON u.id = o.user_id
WHERE o.status = 'paid'
  AND o.created_at >= '2026-01-01'
ORDER BY o.created_at DESC
LIMIT 100

例2: サブクエリを整理 / Indent subqueries

SELECT * FROM (SELECT user_id, SUM(amount) total FROM orders GROUP BY user_id) t WHERE total > 1000

SELECT *
FROM (
  SELECT user_id, SUM(amount) total
  FROM orders
  GROUP BY user_id
) t
WHERE total > 1000

Why browser-only? / なぜブラウザ完結か

A production SQL query reveals table names, column names, business logic, and sometimes literal identifiers of customers in WHERE clauses. Pasting it into a hosted formatter hands that schema snapshot to an external service. DevToolBox formats locally in your browser — nothing leaves the tab.

本番 SQL にはテーブル名、カラム名、ビジネスロジック、WHERE 句にはしばしば顧客識別子が現れます。 ホスト型の整形ツールに貼ると、その時点のスキーマ断片が第三者に渡ります。 DevToolBox はブラウザ内で整形するだけで、タブの外には何も出ません。

関連ガイド / Related Guides

開発をもっと効率的に

PR