Can you filter on a window function result directly in WHERE?
Model answer
No, because window functions are evaluated after WHERE. Put the query in a subquery or CTE and filter in the outer query, for example WHERE rn = 1. Some databases offer QUALIFY, but it is not standard everywhere.
Learn this topic
Window Functions: ROW_NUMBER, RANK and Running Totals in the course SQL for Beginners: Learn Databases Step by Step.
Sign in to add your own answer.