<!--
  canonical-url: https://www.avonture.be/blog/sql-formatter
  published:     2024-07-17
  This is a static plain-Markdown mirror generated at build time.
  Visit the canonical URL above for the fully rendered page, with images and interactive components.
-->

# SQL - Formatting tool

> Easily clean up unreadable, legacy SQL code with a simple formatting tool. Make your SQL queries instantly readable and kickstart your code review process.

---
![SQL - Formatting tool](/img/v2/sql_format.webp)

**TL;DR**

This article introduces the author's free, self-hosted SQL Formatter tool (source on GitHub, hosted demo at sql-formatter.avonture.be), which turns unreadable single-line legacy SQL queries into properly indented, readable statements — a useful first step before reviewing old database code.

When faced with legacy code, it is often useful to reformat it to make it readable.  And from there, the study of the code can begin.

There are plenty of reformatting tools for [json](/blog/json-lint), [php](/blog/online-php-linter), javascript and other languages, but far fewer for a query written in SQL.

Just copy/paste `SELECT LAT_N, CITY, TEMP_F FROM STATS, STATION WHERE MONTH = 7 AND STATS.ID = STATION.ID ORDER BY TEMP_F` in the tool and get

```sql
SELECT
    LAT_N,
    CITY,
    TEMP_F
FROM
    STATS,
    STATION
WHERE
    MONTH = 7
    AND STATS.ID = STATION.ID
ORDER BY
    TEMP_F
```

Retrieve my **SQL Formatter** tool and sources on <https://github.com/cavo789/sql_formatter>.

The tool is accessible online: <https://sql-formatter.avonture.be/>

![Demo](./images/sql_formatter_demo.gif)

I've built a couple of similar self-hosted, single-purpose tools: [JSON - Online linter](/blog/json-lint) and [Excel Formula Beautifier](/blog/excel-formatter).
