Windowing Flashcards
What are SQL window functions?
SQL window functions compute values based on a set of rows related to the current row (called a window or window frame).
Examples include ROW_NUMBER(), SUM(), LEAD(), and NTILE().
The OVER() clause defines the window for these functions.
How do we find duplicates?
GROUP BY with COUNT() and HAVING COUNT() > 1.
Why use windowing functions over subqueries?
Windowing functions provide similar results to subqueries but are more efficient.
How do we order rows?
Use ORDER BY column_name to sort rows.
what is Rank ?
Use RANK() or DENSE_RANK() to assign ranks.
ROW_NUMBER() ?
Returns the position of the row in the result set.
RANK()
Ranks rows based on a given value.
DENSE_RANK()
Ranks rows without leaving gaps.
Analytic functions:
EAD(), LAG(), FIRST_VALUE(): Access data from other rows in the same window.
How do we compare with previous/next rows?
Use LEAD() and LAG() to access adjacent row values.
What are these advanced grouping techniques?
GROUPING SETS, ROLLUP, and CUBE allow flexible aggregation.
They generate multiple levels of subtotals and grand totals.
How Are Window Functions Configured?
Window functions use the OVER clause:
Window partition (PARTITION BY): Groups rows into partitions. Window ordering (ORDER BY): Defines row order within each window. Window frame (ROWS): Defines the window using offsets from a specified row.