Why I Built a SQL Formatter
I often run into messy SQL queries while working with databases. Sometimes queries come from logs, dashboards, or teammates and they’re hard to read because everything is on one line.
Instead of opening an IDE just to clean up the query, I wanted a quick tool where I could paste SQL and instantly get a readable version.
So I built a small SQL formatter.
The tool is available here:
What the Tool Does
The formatter takes a SQL query and returns a formatted version with proper indentation and readable structure.
Current features include:
- indentation control
- keyword case toggle (UPPER / lower / preserve)
- SQL syntax highlighting
- copy formatted SQL
- download formatted SQL as
.sql - automatic formatting on paste
Architecture
I wanted to keep the architecture extremely simple.
Backend
The backend is a small Python service built with FastAPI.
Formatting is handled using the sqlparse library:
python
formatted = sqlparse.format(
sql,
reindent=True,
indent_width=indent_size,
keyword_case=keyword_case
)
The API exposes a single endpoint:
POST /format
which receives SQL and formatting options and returns the formatted query.
Validation
One interesting challenge was validation.
sqlparse will happily accept almost any text and return it unchanged. That means a string like:
would still be “formatted”.
To avoid this, I added a lightweight validation layer that checks whether the input actually resembles a SQL statement before formatting.
⸻
Frontend
The frontend is intentionally minimal:
• vanilla HTML + JavaScript
• syntax highlighting using highlight.js
• copy and download buttons
• simple formatting controls
Keeping the frontend lightweight makes the tool load very quickly.
⸻
Deployment
The service is containerized with Docker and deployed on Railway.
The domain is managed via Cloudflare:
⸻
Next Features
Some improvements I’m considering:
• dialect-specific formatting (PostgreSQL / MySQL / Spark SQL)
• SQL linting
• share formatted SQL via URL
⸻
Feedback Welcome
If you use SQL regularly, I’d love to hear what features would make a formatter like this more useful.
You can try the tool here:
Top comments (0)