Skip to content

distinct-group-by

SQLsuspiciousno fixdefault: warnsql/sqlfluff

Reports SELECT DISTINCT in a query that has GROUP BY.

When the select list has all the grouping columns, the rows are distinct already and DISTINCT does nothing. When it leaves one out (SELECT DISTINCT a ... GROUP BY a, b), DISTINCT does change the result, and the grouping by the other column is probably a mistake.

To fix, remove DISTINCT, or group by the selected columns only.

Reported
...
select a from t where a is null and b is not null and c = 'null';
select distinct a from t group by a;
select distinct a from t;
select a from t group by a;
...
sql:
lint:
rules:
distinct-group-by: warn # off, warn (default), error

Enabled by the presets: sql/sqlfluff.

Suppress one line with sudsy-disable-next-line distinct-group-by in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
sqlfluff AM01 full