Skip to content

ST08 — DISTINCT ON without ORDER BY is non-deterministic

Group: Structure · Auto-fixable: no

DISTINCT ON (...) keeps one row per group, but which row survives is arbitrary unless an ORDER BY pins it down. Add an ORDER BY covering the DISTINCT ON expressions.

bad: SELECT DISTINCT ON (user_id) * FROM events
good: SELECT DISTINCT ON (user_id) * FROM events ORDER BY user_id, ts DESC

Disable this rule in banshee.toml:

[lint]
exclude = ["ST08"]

Or silence one line with -- noqa: ST08. See Linting.