SQL 포매터
PostgreSQL, MySQL, SQLite용 SQL을 정리하고 들여쓰며, 키워드는 대문자로 바꾸고 절마다 줄을 나눕니다. 한 줄로 압축할 수도 있습니다.
브라우저 안에서만 실행됩니다. 입력한 내용은 어디에도 업로드되지 않습니다.
SELECT c.id, c.email, count(o.id) AS order_count, sum(o.total) AS lifetime_value FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.created_at >= '2026-01-01' AND o.status <> 'refunded' GROUP BY c.id, c.email HAVING count(o.id) > 3 ORDER BY lifetime_value DESC LIMIT 25;
절마다 줄을 나누고 SELECT 목록, GROUP BY, ON CONFLICT, 서브쿼리를 들여씁니다. PostgreSQL, MySQL, SQLite 키워드를 모두 인식합니다.
SQL 정리 참고
빠른 참조- SELECT … FROM … WHERE
- SQL이 기대하는 절 순서입니다. 절마다 줄을 나누면 긴 쿼리도 리뷰나 로그에서 읽기 쉬워집니다.
- LEFT JOIN … ON
- 왼쪽 테이블의 모든 행을 유지하고 일치하지 않으면 NULL로 채웁니다. INNER JOIN은 일치하지 않는 행을 버립니다.
- GROUP BY / HAVING
- HAVING은 집계 후에 거르고 WHERE는 집계 전에 행을 거릅니다. 그래서 WHERE에는 COUNT나 SUM 결과를 쓸 수 없습니다.
- WITH name AS (…)
- 공통 테이블 식(CTE)입니다. 대부분의 플래너가 인라인 처리하지만 긴 쿼리를 읽기 좋게 유지합니다. 자기 참조에는 RECURSIVE를 붙입니다.
- ON CONFLICT (key) DO UPDATE
- PostgreSQL과 SQLite의 upsert 문법입니다. MySQL은 INSERT … ON DUPLICATE KEY UPDATE를 씁니다.
- LIMIT n OFFSET m
- 결과를 나눠 볼 때 씁니다. 오프셋이 수천 행을 넘으면 키셋 커서(WHERE id > last_id)가 더 낫습니다.
SQL 관련 문법 가이드
SQL 가이드 전체Postgres RLS 42P17: infinite recursion detected in policy
A policy on members that reads members re-enters itself. One security definer function with a pinned search_path ends the recursion and leaves the rest of the policy intact.
SQLPgBouncer Transaction vs Session Pooling: What Breaks
Port 6543 hands out a server connection per transaction, so prepared statements, SET, LISTEN and cursors stop behaving. Driver-level settings for each stack.
SQLZero-Downtime Postgres Migrations: lock_timeout and NOT VALID
One ALTER TABLE waiting behind a slow SELECT blocks every query that arrives after it. The timeout-retry loop, NOT VALID constraints and CONCURRENTLY index rules.
SQLPostgres JSONB Indexing: GIN vs Expression B-Tree
GIN answers containment, not equality, and an expression B-tree is the reverse. Choose by operator, match the expression exactly, and prove it with EXPLAIN.