EXCLUDE is part of the window frame clause that controls which of the rows inside an already defined frame are visible to the window function at each step. After the frame is built with PARTITION BY, ORDER BY, and a frame boundary (ROWS, RANGE, or GROUPS), the optional EXCLUDE modifier removes specific rows before the function is evaluated:- EXCLUDE CURRENT ROW – drops the row that is producing the result.- EXCLUDE GROUP – drops all rows that are peers of the current row (same ordering keys).- EXCLUDE TIES – drops peers that tie with the current row on the ORDER BY keys but keeps the current row itself.- EXCLUDE NO OTHERS – default; keeps every row in the frame.Because EXCLUDE is evaluated after the frame boundaries are resolved, it never changes frame start or end points; it only masks rows for the function call. Aggregate and ranking window functions, as well as user-defined window functions, honor the exclusion set. Implementations that support EXCLUDE usually also support GROUPS frames, as both were standardized together in SQL:2011.Caveats:1. Not all databases implement EXCLUDE even if they support window functions.2. Some engines restrict EXCLUDE to GROUPS frames or prohibit it with RANGE frames lacking ORDER BY.3. Performance can degrade on large peer groups because engines must determine ties per row.
CURRENT ROW
(keyword) - excludes only the current row.GROUP
(keyword) - excludes all rows that share the ORDER BY values with the current row.TIES
(keyword) - excludes tied rows except the current row.NO OTHERS
(keyword) - default; retains all rows.frame_start/frame_end
- expressions defining the frame boundaries (not unique to EXCLUDE).WINDOW, OVER, PARTITION BY, ROWS, RANGE, GROUPS, ORDER BY
SQL:2011 standard; first implemented in PostgreSQL 14
It excludes the row currently being processed from the window frame, useful for prior running totals or averages.
No. Neither MySQL nor SQL Server support EXCLUDE as of 2024, even though they support other window function features.
No. It filters rows after the frame is established; the start and end points remain unchanged.
GROUP removes all peer rows including the current one. TIES removes only peers with equal ORDER BY values, keeping the current row in the frame.