Skip to main content

Clause Newlines

Major SQL clauses (FROM, WHERE, GROUP BY, ORDER BY, JOIN, etc.) should each start on their own line.

Affected clauses: FROM, WHERE, HAVING, GROUP BY, ORDER BY, SORT BY, LIMIT, WINDOW, QUALIFY, RETURNING, JOIN (all types), FETCH, MERGE WHEN, VALUES, SET, UNION/INTERSECT/EXCEPT. Note: INSERT INTO stays on one line.

Simple SELECT with FROM​

select a, b, c from t
select a, b, c from t

SELECT with WHERE​

select a from t where x > 1
select a from t where x > 1

SELECT with GROUP BY and ORDER BY​

select a, count(*) from t group by a order by a
select a, count(*) from t group by a order by a

SELECT with HAVING​

select a, count(*) from t group by a having count(*) > 1
select a, count(*) from t group by a having count(*) > 1

SELECT with LIMIT​

select a from t limit 10
select a from t limit 10

UNION ALL between two SELECTs​

Set operators should start on their own line like other clauses.

select a, b from t1 where x > 1 union all select c, d from t2 where y < 2
select a, b from t1 where x > 1 union all select c, d from t2 where y < 2

UNION inside subquery​

select * from (select a from t1 union select b from t2) as combined
select * from (select a from t1 union select b from t2) as combined
select * from (select a from t1 union select b from t2) as combined
select *
from (
    select a
    from t1
    union
    select b
    from t2
) as combined

Mixed UNION and INTERSECT​

select a from t1 union select b from t2 intersect select c from t3
select a from t1 union select b from t2 intersect select c from t3

Recursive CTE with UNION ALL​

UNION ALL inside a CTE body should get its own line.

with recursive nums as (select 1 as n union all select n + 1 from nums where n < 10) select n from nums
with recursive
    nums as (select 1 as n union all select n + 1 from nums where n < 10)
select n
from nums
with recursive nums as (select 1 as n union all select n + 1 from nums where n < 10) select n from nums
with recursive
    nums as (
        select 1 as n
        union all
        select n + 1
        from nums
        where n < 10
    )
select n
from nums

SELECT 1 trivial​

select 1
select 1

SELECT NULL trivial​

select null
select null

BETWEEN expression​

BETWEEN in WHERE clause — AND is part of the BETWEEN syntax, not a boolean operator.

select a from t where x between 1 and 10
select a from t where x between 1 and 10

EXISTS subquery​

select a from t where exists (select 1 from t2 where t2.id = t.id)
select a from t where exists(select 1 from t2 where t2.id = t.id)
select a from t where exists (select 1 from t2 where t2.id = t.id)
select a
from t
where exists(
    select 1
    from t2
    where t2.id = t.id
)

Window function OVER​

Window function with OVER clause.

select row_number() over (partition by a order by b) from t
select row_number() over (partition by a order by b) from t

Window function with frame​

Window function with frame specification.

select sum(a) over (order by b rows between unbounded preceding and current row) from t
select sum(a) over (order by b rows between unbounded preceding and current row)
from t

Simple JOIN​

select a from t1 join t2 on t1.id = t2.id
select a from t1 join t2 on t1.id = t2.id

Multiple JOINs​

select a from t1 join t2 on t1.id = t2.id left join t3 on t2.id = t3.id
select a from t1 join t2 on t1.id = t2.id left join t3 on t2.id = t3.id

JOIN at same level as FROM​

JOIN clauses render at the same indentation level as FROM.

select a from t1 join t2 on t1.id = t2.id
select a from t1 join t2 on t1.id = t2.id

Multiple JOINs at same level as FROM​

select a from t1 join t2 on t1.id = t2.id left join t3 on t2.id = t3.id
select a from t1 join t2 on t1.id = t2.id left join t3 on t2.id = t3.id

JOIN with AND in ON​

select a from t1 join t2 on t1.id = t2.id and t1.x = t2.x
select a from t1 join t2 on t1.id = t2.id and t1.x = t2.x

Extra spaces normalized​

Extra whitespace between tokens is collapsed to standard spacing.

select   a ,  b   from   t   where   x  >  1
select a, b from t where x > 1

Tabs normalized to spaces​

select	a,	b	from	t	where	x	>	1
select a, b from t where x > 1

Newlines in wrong places​

Broken mid-clause — the formatter restructures clauses onto proper lines.

select a,
b from
t where
x > 1 and
y < 2
select a, b from t where x > 1 and y < 2

THEN starts its own line when the condition broke​

Whether THEN got its own line used to depend only on whether THEN plus its result would overrun the budget from wherever the condition happened to end. After a condition that broke, the last line is short, so there was room and THEN glued to it — reading as part of the condition: a when line ending and hendelse in ('INNKALT', 'NYTT_TID_STED') then to_date('2022-01-06', 'YYYY-MM-DD'), with the then buried mid-line.

A single CASE would then alternate between the two shapes, arm by arm, depending on how each condition's last line landed. An arm that is multi-line at all puts THEN on its own line, at the WHEN keyword's level.

select case when a = 1 and b in ('xxxxxxxxxx', 'yyyyyyyyyy', 'zzzzzzzzzz') then 2 else 3 end as v from t
select
    case
        when a = 1 and b in ('xxxxxxxxxx', 'yyyyyyyyyy', 'zzzzzzzzzz')
        then 2
        else 3
    end as v
from t