1# SQL
2
3SQL files are handled by the [SQL Extension](https://github.com/zed-extensions/sql).
4
5- Tree-sitter: [nervenes/tree-sitter-sql](https://github.com/nervenes/tree-sitter-sql)
6
7### Formatting
8
9Zed supports auto-formatting SQL using external tools like [`sql-formatter`](https://github.com/sql-formatter-org/sql-formatter).
10
111. Install `sql-formatter`:
12
13```sh
14npm install -g sql-formatter
15```
16
172. Ensure `sql-formatter` is available in your path and check the version:
18
19```sh
20which sql-formatter
21sql-formatter --version
22```
23
243. Configure Zed to automatically format SQL with `sql-formatter`:
25
26```json [settings]
27 "languages": {
28 "SQL": {
29 "formatter": {
30 "external": {
31 "command": "sql-formatter",
32 "arguments": ["--language", "mysql"]
33 }
34 }
35 }
36 },
37```
38
39Substitute your preferred [SQL Dialect] for `mysql` above (`duckdb`, `hive`, `mariadb`, `postgresql`, `redshift`, `snowflake`, `sqlite`, `spark`, etc).
40
41You can add this to Zed project settings (`.zed/settings.json`) or via your Zed user settings (`~/.config/zed/settings.json`).
42
43### Advanced Formatting
44
45Sql-formatter also allows more precise control by providing [sql-formatter configuration options](https://github.com/sql-formatter-org/sql-formatter#configuration-options). To provide these, create a `.sql-formatter.json` file in your project:
46
47```json [settings]
48{
49 "language": "postgresql",
50 "tabWidth": 2,
51 "keywordCase": "upper",
52 "linesBetweenQueries": 2
53}
54```
55
56When using a `.sql-formatter.json` file you can use a more simplified set of Zed settings since the language need not be specified inline:
57
58```json [settings]
59 "languages": {
60 "SQL": {
61 "formatter": {
62 "external": {
63 "command": "sql-formatter"
64 }
65 }
66 }
67 },
68```