How to format SQL in VS Code
Visual Studio Code has no built-in SQL formatter. Shift+Alt+F is wired to whichever extension has registered itself as the formatter for the current language, and out of the box nothing has registered for SQL — which is why the command appears to do nothing on a .sql file and why the answer to this question is always an extension recommendation. The complication is that the extension most tutorials still name has been unmaintained for years and has an official successor.
Short answer
Not built in — install an extension
Install SQL Formatter VSCode (publisher ReneSaarsoo), register it as the default formatter for the sql language in settings.json, then Shift+Alt+F works as expected. It is the official extension of the sql-formatter library and the maintained successor to Prettier SQL VSCode.
- Windows
- Shift+Alt+F
- macOS
- Shift+Option+F
Step by step in Visual Studio Code
- 01
Install the maintained extension
In the Extensions view search for “SQL Formatter” and pick the one published by Rene Saarsoo (ReneSaarsoo.sql-formatter-vsc). It is the official VS Code extension of the sql-formatter library. The older Prettier SQL VSCode by inferrinizzard is no longer maintained and its own marketplace page now points here — if you already have it, uninstall it rather than running both.
- 02
Register it for the sql language
Open settings.json and add a language-scoped default formatter, otherwise VS Code will not know which extension to run: "[sql]": { "editor.defaultFormatter": "ReneSaarsoo.sql-formatter-vsc" }. Without this entry Shift+Alt+F either does nothing or prompts you to choose a formatter every time.
- 03
Turn on format-on-save for SQL only
Add "editor.formatOnSave": true inside that same [sql] block rather than globally. Scoping it means you get formatted SQL without also reformatting every other file type in a repository that may have its own conventions.
- 04
Set the dialect
The extension carries the sql-formatter grammars, so tell it which one you write: the SQL-formatter: Dialect setting accepts PostgreSQL, MySQL, T-SQL, BigQuery, Snowflake, Redshift, Spark and others. Leaving it on the generic default is the single most common reason output looks subtly wrong — dialect-specific syntax gets treated as unknown tokens.
- 05
For T-SQL specifically, consider the Microsoft extension
The official SQL Server (mssql) extension brings T-SQL formatting alongside connections, IntelliSense and query execution. If SQL Server is all you touch, it may be all you need; if you move between engines, the sql-formatter extension covers more ground.
PostgreSQL, before and after
The unformatted query below is the sample loaded into the editor above, and the output is exactly what this formatter returns for it with the default options. Nothing was tidied up by hand.
WITH monthly AS (SELECT date_trunc('month', e.created_at) AS month, e.project_id, count(*) AS events FROM analytics.events AS e WHERE e.created_at >= $1 AND e.event_type = ANY($2) GROUP BY 1, 2) SELECT p.slug, m.month, m.events, lag(m.events) OVER (PARTITION BY p.slug ORDER BY m.month) AS prev_events FROM monthly AS m JOIN public.projects AS p ON p.id = m.project_id WHERE m.events > $3 ORDER BY p.slug, m.month; WITH
monthly AS (
SELECT
date_trunc('month', e.created_at) AS MONTH,
e.project_id,
count(*) AS events
FROM
analytics.events AS e
WHERE
e.created_at >= $1
AND e.event_type = ANY ($2)
GROUP BY
1,
2
)
SELECT
p.slug,
m.month,
m.events,
lag(m.events) OVER (
PARTITION BY
p.slug
ORDER BY
m.month
) AS prev_events
FROM
monthly AS m
JOIN public.projects AS p ON p.id = m.project_id
WHERE
m.events > $3
ORDER BY
p.slug,
m.month; What VS Code still won't do
None of this is a knock on VS Code — these are just the edges of what an in-editor formatter is for. They are also the reason there is a formatter at the top of this page.
- SQL embedded in another language is not formatted. A query inside a Python triple-quoted string or a TypeScript template literal is just string content to VS Code, and no SQL formatter extension will touch it.
- Configuration is JSON with no preview. You edit settings.json, reformat, look at the result, and repeat — there is no side-by-side view of what a given option does.
- Extensions are a non-starter on a machine where you cannot install them, which covers a lot of client laptops and jump boxes.
- Nothing in the extension helps with parameter placeholders or with reading a deeply nested WHERE clause.
VS Code SQL formatting FAQ
Why does Shift+Alt+F do nothing on my .sql file?
Because no extension has registered as the formatter for the sql language. Format Document is a dispatcher, not a formatter — install a SQL formatter extension and add a "[sql]" default-formatter entry to settings.json, and the shortcut starts working.
How do I use a SQL formatter in VS Code?
Install SQL Formatter VSCode by Rene Saarsoo, set it as editor.defaultFormatter inside the [sql] block in settings.json, then run Format Document with Shift+Alt+F on Windows or Shift+Option+F on macOS. It is the official extension of the sql-formatter library and the maintained successor to Prettier SQL VSCode.
Does Prettier format SQL files?
Not by default. Prettier has no built-in SQL parser, and support for .sql files remains an open request on the project. A dedicated SQL formatter extension is the reliable route.
How do I format SQL inside a Python or TypeScript string in VS Code?
You cannot, with a SQL formatter extension — the editor sees a string literal, not SQL. Paste the query into a formatter like the one above, format it, and paste it back. That is also where the parameter placeholders your ORM left behind stop being a problem.
Can I make VS Code format SQL on save?
Yes. Put "editor.formatOnSave": true inside the "[sql]" block in settings.json so it applies to SQL files only, rather than turning it on globally and reformatting everything else in the repository.
Will the output match what this page's formatter produces?
Very closely — the extension and this site both use the sql-formatter library, so the same dialect and options give the same output. You can tune settings here where the effect is visible immediately, then mirror them into settings.json.
Verified 2026-08-20 against