How to format SQL in IntelliJ and DataGrip
JetBrains ships the most configurable SQL formatter of any editor here, and it is the same engine in IntelliJ IDEA Ultimate, DataGrip, PyCharm Professional and the rest of the family. It is dialect-aware, it has predefined style presets, and the settings screen previews your changes live. The two things worth knowing before you go looking for it: SQL support is absent from the free Community edition, and the formatter only does its best work once the file has a SQL dialect assigned to it.
Short answer
Built in, and the most configurable of the lot
Press Ctrl+Alt+L (Cmd+Option+L on macOS) to reformat, and configure it under Settings → Editor → Code Style → SQL, choosing the dialect you want to configure at the top of the page.
- Windows
- Ctrl+Alt+L
- macOS
- Cmd+Option+L
Step by step in IntelliJ IDEA and DataGrip
- 01
Reformat
Ctrl+Alt+L on Windows and Linux, Cmd+Option+L on macOS, or Code → Reformat Code from the menu. As everywhere in the JetBrains IDEs it applies to the selection when there is one and the whole file when there is not.
- 02
Open the SQL code style
Ctrl+Alt+S opens Settings, then Editor → Code Style → SQL. The first thing on that page is a dialect selector — the settings you are about to change apply to the dialect you pick, so Postgres and Oracle can each have their own conventions in the same project.
- 03
Start from a predefined style
Rather than working through every option, use the Set from… control to load a preset: Modern is the sensible default, and Joe Celko Allman, Whitesmiths, Egypt and Old Idea are there for teams with an existing house style. Adjust from whichever lands closest; the preview pane updates as you go.
- 04
Assign the dialect to the file
A .sql file with no dialect assigned gets generic treatment. Set it per data source, per folder, or via the dialect widget in the editor's status bar. This is the single most common reason IntelliJ's SQL formatting looks worse than it should.
- 05
Inject SQL into host-language strings
For SQL living inside a Java, Kotlin or Python string, use language injection — Alt+Enter on the literal and Inject language or reference → SQL. Once injected, the string gets highlighting, completion and, importantly, Reformat Code. It is the only editor in this list that solves that problem properly.
Snowflake, 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 scored AS (SELECT a.account_id, a.plan_tier, f.feature_key, SUM(f.usage_units) AS units FROM analytics.feature_usage AS f JOIN analytics.accounts AS a ON a.account_id = f.account_id WHERE f.usage_date BETWEEN :start_date AND :end_date GROUP BY 1, 2, 3) SELECT plan_tier, feature_key, SUM(units) AS total_units, RATIO_TO_REPORT(SUM(units)) OVER (PARTITION BY plan_tier) AS share FROM scored WHERE plan_tier = :plan_tier ORDER BY plan_tier, total_units DESC; WITH
scored AS (
SELECT
a.account_id,
a.plan_tier,
f.feature_key,
SUM(f.usage_units) AS units
FROM
analytics.feature_usage AS f
JOIN analytics.accounts AS a ON a.account_id = f.account_id
WHERE
f.usage_date BETWEEN:start_date AND:end_date
GROUP BY
1,
2,
3
)
SELECT
plan_tier,
feature_key,
SUM(units) AS total_units,
RATIO_TO_REPORT(SUM(units)) OVER (
PARTITION BY
plan_tier
) AS share
FROM
scored
WHERE
plan_tier =:plan_tier
ORDER BY
plan_tier,
total_units DESC; What IntelliJ still won't do
None of this is a knock on IntelliJ — 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 support is not in IntelliJ IDEA Community. It requires Ultimate, DataGrip, or another paid JetBrains IDE — for a lot of people this is the whole answer to why the option is not there.
- Language injection has to be set up per string or per pattern; a query assembled from concatenated fragments will not be recognised as SQL at all.
- The formatter reformats code, it does not explain it. A 200-line query with six CTEs comes out beautifully indented and no easier to reason about.
- Nothing here does anything with the parameter placeholders left in a query pulled out of application logs.
IntelliJ SQL formatting FAQ
What is the shortcut to format SQL in IntelliJ or DataGrip?
Ctrl+Alt+L on Windows and Linux, Cmd+Option+L on macOS. It runs Code → Reformat Code on the selection, or on the whole file if nothing is selected.
Where are the SQL formatting settings in IntelliJ?
Settings → Editor → Code Style → SQL, reachable with Ctrl+Alt+S. Choose the SQL dialect at the top of the page first — the options below it apply to that dialect only.
Why is there no SQL option in my IntelliJ code style settings?
You are almost certainly on IntelliJ IDEA Community, which has no SQL or database support. The formatter comes with Ultimate, DataGrip and the other paid IDEs in the family.
How do I make IntelliJ uppercase SQL keywords?
It is a code style setting rather than a separate command: Settings → Editor → Code Style → SQL, pick your dialect, and set the keyword case option there. The next Reformat Code applies it.
Can IntelliJ format SQL inside a Java or Python string?
Yes, via language injection — put the caret in the string, press Alt+Enter and choose Inject language or reference → SQL. After that the string formats with the rest of the file. It will not work on a query built by concatenating fragments.
What are the predefined SQL styles in DataGrip?
Modern, Joe Celko Allman (DDL only), Whitesmiths (DDL only), Egypt and Old Idea. Load one with the Set from… control on the Code Style → SQL page and adjust from there rather than configuring every option by hand.
Verified 2026-08-20 against