QUALIFY Clause (Snowflake)
QUALIFY Clause (Snowflake) is a SQL function in the Window Functions category. Snowflake: allows filtering on window function results directly in the WHERE-equivalent QUALIFY clause. The syntax is SELECT columns, window_func() OVER (...) AS alias FROM table QUALIFY alias condition;. It accepts .... It returns top N per group (Snowflake). A typical example: SELECT name, department, salary, ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rank FROM employees QUALIFY rank = 1; -- Simplest way to get top earner per department -- No need for subquery or CTE SELECT name, department, salary FROM employees QUALIFY ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) = 1; -- Even simpler: inline the window function A close relative is ROW_NUMBER(), which assigns a unique sequential integer to each row within its partition (1, 2, 3...). A close relative is RANK(), which similar to ROW_NUMBER but gives the same rank to equal values. Leaves gaps in ranking.