# SQL Formatter

URL: https://phishdestroy.io/developer-tools/sql-formatter
Category: General dev
Where it runs: Runs locally (nothing you enter is uploaded)

Make a long one-line SQL query readable: clause keywords such as SELECT, FROM, JOIN and WHERE start new lines and comma-separated columns line up. Minify does the reverse.

## Example input

One-line join query

```
SELECT d.domain, r.reported_at FROM reports r JOIN domains d ON d.id = r.domain_id WHERE r.status = 'open' AND r.score > 80 ORDER BY r.reported_at DESC LIMIT 20
```

SELECT, FROM, JOIN, ON, WHERE, AND, ORDER BY and LIMIT each start a new line, and the two selected columns go on separate lines.

## FAQ

### How does it decide where to break lines?

It looks for a fixed list of 40 keywords and phrases (SELECT, FROM, JOIN, ON, WHERE, AND, OR, AS, IN, GROUP BY, ORDER BY, LIMIT and others), starts a new line before each one and upper-cases it, and adds a line break after every comma. It does not parse SQL, so there is no syntax check and subqueries are not indented.

### Can formatting change what my query does?

Yes, in two cases. Keywords and commas inside quoted strings are changed too (WHERE note = 'select from' gets line breaks inside the string), and Minify keeps -- comments, which then hide the rest of the one-line query. Check string literals and remove line comments before you use the result.

### Is my SQL sent to a server?

No. Formatting is a set of text replacements done in your browser; the query is not uploaded or stored.

## Related tools

- [XML Formatter](https://phishdestroy.io/developer-tools/xml-formatter)
- [YAML Formatter](https://phishdestroy.io/developer-tools/yaml-formatter)
- [JSON Formatter](https://phishdestroy.io/developer-tools/json-formatter)
- [JSON & JavaScript String Escaper](https://phishdestroy.io/developer-tools/string-escaper)
- [Text Diff Viewer](https://phishdestroy.io/developer-tools/text-diff)

All tools: https://phishdestroy.io/developer-tools
