Skip to main content

AND/OR Formatting

AND/OR operators in WHERE, HAVING, and JOIN ON conditions start new lines.

Scope: AND/OR newlines apply in WHERE, HAVING, and JOIN ON contexts. AND/OR inside CASE WHEN conditions is suppressed (stays inline) to avoid overly fragmented WHEN clauses.

clauseLayout interaction: When clauseLayout=indented (default), AND/OR lines are indented by 1 space under the clause keyword. When clauseLayout=left_aligned, AND/OR starts at the same indent level as the clause.

WHERE with AND

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

WHERE with AND and OR

select a from t where x > 1 and y < 2 or z = 3
select a from t where (x > 1 and y < 2) or z = 3

HAVING with AND

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

JOIN ON with AND

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

AND/OR NEVER mode keeps inline

NEVER mode keeps AND/OR inline even with vertical input.

select a from t where x > 1
and y < 2
or z = 3
select a from t where (x > 1 and y < 2) or z = 3

AND/OR ALWAYS mode expands

ALWAYS mode (default) expands AND/OR even from compact input.

select a from t where x > 1 and y < 2 or z = 3
select a from t where (x > 1 and y < 2) or z = 3

AND/OR WIDTH_AWARE compact

WIDTH_AWARE mode: user wrote compact — keep compact.

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

AND/OR WIDTH_AWARE vertical

WIDTH_AWARE mode: user wrote vertical (newline before AND) — expand.

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

AND/OR WIDTH_AWARE mixed preserves per keyword

WIDTH_AWARE mode: each AND/OR keyword independently detected. First AND has newline, second doesn't.

select a from t where x > 1
and y < 2 and z = 3
select a from t where x > 1 and y < 2 and z = 3

HAVING with AND/OR

select dept, count(*) from employees group by dept having count(*) > 5 and sum(salary) > 100000 or dept = 'EXEC'
select dept, count(*)
from employees
group by dept
having (count(*) > 5 and sum(salary) > 100000) or dept = 'EXEC'

AND/OR AFTER mode — WHERE

AFTER mode: AND/OR ends the line, continuation on next line.

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

AND/OR AFTER mode — WHERE with multiple

select a from t where x > 1 and y < 2 or z = 3
select a from t where (x > 1 and y < 2) or z = 3

AND/OR AFTER mode — HAVING

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

AND/OR AFTER mode — JOIN 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

Complex JOIN ON with multiple ANDs

Multiple AND conditions in JOIN ON each get their own line.

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