SQL

SQL Formatter & Minifier

Format common SQL statements or compress them to one line

SQL Formatter & Minifier — what it does

Format SQL statements with consistent line breaks and indentation so their structure is visible, or compress them to a single line for embedding. A long query written as one unbroken string is difficult to review; the same query with its clauses on separate lines can be read like an outline.

Worked example

A report query pasted as four loose lines, with a function call, a -- comment and a BETWEEN range, run through Format. Keywords are uppercased, each clause starts a new line, and AND conditions and top-level columns are indented beneath it.

Input — SQL

select o.id, o.total, coalesce(c.name, 'walk-in') as customer -- fallback name
from orders o left outer join customers c on c.id = o.customer_id
where o.status = 'open' and o.created_at between '2026-10-01' and '2026-10-20'
order by o.total desc limit 20

Output — formatted

SELECT o.id,
  o.total,
  COALESCE(c.name, 'walk-in') AS customer -- fallback name
FROM orders o
LEFT OUTER JOIN customers c ON c.id = o.customer_id
WHERE o.status = 'open'
  AND o.created_at BETWEEN '2026-10-01' AND '2026-10-20'
ORDER BY o.total DESC
LIMIT 20

COALESCE(c.name, 'walk-in') stays on one line, LEFT OUTER JOIN is kept together, the comment ends its own line, and the AND inside BETWEEN … AND is not broken; the quoted dates are unchanged.

When this tool helps

  • Making an inherited query readable before modifying it.
  • Tidying SQL before committing it or including it in a code review.
  • Compressing a query into one line for a configuration file or a log entry.

How to use it

  1. Paste the SQL into the input box.
  2. Choose format or minify.
  3. Review the output.
  4. Copy the result back into your editor or tool.

Accuracy, limits, and good practice

  • Formatting rearranges whitespace only, so the query's behaviour is unchanged — but always re-test before running a reformatted statement on production data.
  • Vendor-specific extensions and unusual syntax may format less tidily than standard SQL; check the output rather than trusting it blindly.
  • Keep the readable version in source control and let tools do the compressing; committing single-line queries makes future diffs unreadable.

Frequently asked questions

Does formatting change what the query does?

No, only layout changes. Keywords, clauses and their order are preserved exactly.

Which SQL dialects are supported?

Common statement structures across standard SQL. Heavily vendor-specific syntax may not be recognised as neatly.

Are clauses inside a subquery put on their own lines?

No. Line breaks are added only at the top level; everything inside parentheses stays on one line with its keywords uppercased. WHERE customer_id in (select id from customers where region = 'EU') comes out as WHERE customer_id IN (SELECT id FROM customers WHERE region = 'EU').

What does Minify do with comments and quoted text?

Minify removes -- and /* */ comments, collapses whitespace and drops the spaces around commas, brackets and = signs, but it never touches quoted text and keeps the original letter case. select a, 'call -- later' as note, "Order Date" -- check / from t /* x */ where a = 1 became select a,'call -- later' as note,"Order Date" from t where a=1: the string with -- inside and the double space in the quoted identifier survive.

Related tools on this site

Everything above runs inside this page. SQL Formatter & Minifier needs no account, no upload, and no server round trip — close the tab and nothing is left behind.

SQL 格式化與壓縮能做什麼

把 SQL 語句排成一致的斷行與縮排,讓結構一目了然,或壓成單行以便嵌入。寫成一整串的長查詢很難審閱;同一段查詢把各子句分行後,讀起來就像大綱。

實際範例

一段四行的報表查詢,含函式呼叫、-- 註解與 BETWEEN 範圍,按「格式化」整頓。關鍵字一律轉成大寫,每個子句各佔一行,AND 條件與最外層的欄位縮排在其下。

來源:SQL

select o.id, o.total, coalesce(c.name, 'walk-in') as customer -- fallback name
from orders o left outer join customers c on c.id = o.customer_id
where o.status = 'open' and o.created_at between '2026-10-01' and '2026-10-20'
order by o.total desc limit 20

格式化後

SELECT o.id,
  o.total,
  COALESCE(c.name, 'walk-in') AS customer -- fallback name
FROM orders o
LEFT OUTER JOIN customers c ON c.id = o.customer_id
WHERE o.status = 'open'
  AND o.created_at BETWEEN '2026-10-01' AND '2026-10-20'
ORDER BY o.total DESC
LIMIT 20

COALESCE(c.name, 'walk-in') 維持在同一行,LEFT OUTER JOIN 不會被拆開,註解在自己那行結束,BETWEEN … AND 裡的 AND 也不會換行;引號內的日期維持原樣。

什麼時候用得上

  • 修改接手來的查詢前,先讓它變得可讀。
  • 提交或送審程式碼前先整理 SQL。
  • 把查詢壓成一行,放進設定檔或日誌。

使用步驟

  1. 把 SQL 貼進輸入框。
  2. 選擇格式化或壓縮。
  3. 檢視輸出。
  4. 把結果複製回編輯器或工具中。

精確度、限制與實務建議

  • 格式化只重排空白,查詢行為不變——但重排後的語句在正式資料上執行前,仍請重新測試。
  • 廠商特有的擴充語法排版可能不如標準 SQL 整齊,請檢查輸出而非盲目信任。
  • 版本控制中保留可讀版本、壓縮交給工具;提交單行查詢會讓日後的差異比對難以閱讀。

常見問題

格式化會改變查詢行為嗎?

不會,只改版面。關鍵字、子句與順序都完整保留。

支援哪些 SQL 方言?

標準 SQL 的常見語句結構。高度廠商特有的語法可能無法被同樣整齊地識別。

子查詢裡的子句也會各自換行嗎?

不會。只有最外層會換行,括號裡的內容都留在同一行,只是關鍵字會轉成大寫。WHERE customer_id in (select id from customers where region = 'EU') 會變成 WHERE customer_id IN (SELECT id FROM customers WHERE region = 'EU')。

「壓縮」會怎麼處理註解與引號內的文字?

「壓縮」會拿掉 -- 與 /* */ 註解、縮減空白,並去掉逗號、括號與等號兩側的空格,但不會動到引號內的文字,也維持原本的大小寫。select a, 'call -- later' as note, "Order Date" -- check / from t /* x */ where a = 1 會變成 select a,'call -- later' as note,"Order Date" from t where a=1:含 -- 的字串與引號識別字裡的兩個空格都保留下來。

本站相關工具

以上步驟全部在這個頁面內完成,SQL 格式化與壓縮 不需要註冊、不上傳檔案、也不經過伺服器,關閉分頁後不會留下任何資料。