Semi-structured & modern warehouse SQL
Concept  ·  SQL & Querying

QUALIFY filters window results inline (dialect); GROUP BY ALL and friends are conveniences, not concepts.

Each sugar names a ritual: QUALIFY = the subquery-then-filter of top_n_per_group; GROUP BY ALL = 'group by everything I didn't aggregate' (with the caution that an added SELECT column silently changes the grouping); EXCLUDE = 'all but these'. The portability tax is the second half: none is universal — code that ships to Postgres expands QUALIFY back to the subquery, ALL back to the explicit list, EXCLUDE back to named columns.

See it break
carts
carttwistn
eplain18
esalt5
wplain10
The trap
SELECT
  cart,
  twist
FROM
  carts
QUALIFY
  ROW_NUMBER() OVER (
        PARTITION BY
          cart
        ORDER BY
          n DESC
      ) = 1
ORDER BY
  cart;
The fix
SELECT
  cart,
  twist
FROM
  (
    SELECT
      cart,
      twist,
      ROW_NUMBER() OVER (
        PARTITION BY
          cart
        ORDER BY
          n DESC
      ) AS rn
    FROM
      carts
  )
WHERE
  rn = 1
ORDER BY
  cart;
INPUTYour queryThe fix
eplainplain
wplainplain

Both return the best twist per cart, but QUALIFY is engine sugar — it filters the window inline where DuckDB/Snowflake/BigQuery allow it, and code that ships to Postgres must expand it back to the subquery-then-filter.

The rule

Use them where they live; keep the portable expansion next to anything that travels.

QUALIFY / GROUP BY ALL / SELECT * EXCLUDE in DuckDB/Snowflake/BigQuery; Postgres needs the explicit expansions.

Shows up elsewhere
Top-N per groupNon-aggregated column in SELECT
Try one →Prove it in practice →