sqlcmd.net validated sql reference

#window-functions

Commands grouped around the same concept, pattern, or recurring problem.

13 commands with this tag Browse related patterns quickly
advanced windowing MySQL MariaDB SQL Server PostgreSQL SQLite

Access Previous And Next Rows With LAG And LEAD

Reference values from preceding or following rows within an ordered partition without writing a self-join.

advanced windowing MySQL MariaDB SQL Server PostgreSQL SQLite

Access The First Row In A Window With FIRST_VALUE

Return a value from the first row of a window partition, useful for comparing each row against the group leader.

advanced windowing MySQL MariaDB SQL Server PostgreSQL SQLite

Assign Session IDs Based on Inactivity Gaps

Group a stream of user events into sessions by labelling each event with a session ID that increments whenever the gap since the previous event exceeds a threshold.

advanced windowing MySQL MariaDB SQL Server PostgreSQL SQLite

Calculate A Running Total With SUM OVER

Accumulate a column's values row by row using `SUM` as a window function.

advanced windowing MySQL MariaDB SQL Server PostgreSQL

Calculate Percent Of Total With A Window Function

Use `SUM(...) OVER ()` to compare each row's value to the total without collapsing the result set.

advanced windowing MySQL MariaDB SQL Server PostgreSQL SQLite

Compare a Metric Year-Over-Year Using LAG

Pull the prior year's value into the current row with `LAG`, then compute the percentage change — a common pattern for revenue, signups, and other business KPIs.

advanced windowing MySQL MariaDB SQL Server PostgreSQL SQLite

Compute a Rolling Moving Average With a Window Frame

Smooth time-series data by computing a rolling average over a fixed window of preceding rows using `AVG … OVER (ORDER BY … ROWS BETWEEN n PRECEDING AND CURRENT ROW)`.

advanced deduplication MySQL MariaDB SQL Server PostgreSQL SQLite

Delete Duplicate Rows While Keeping One

Use `ROW_NUMBER()` to identify duplicate rows and delete every copy except the one you want to keep.

advanced windowing MySQL MariaDB SQL Server PostgreSQL SQLite

Group Consecutive Rows With A ROW_NUMBER Difference

Create stable run groups without a primary key by subtracting two `ROW_NUMBER()` calculations over the same ordered events.

advanced windowing MySQL MariaDB SQL Server PostgreSQL SQLite

Normalise Values to Z-Scores

Rescale each row's value to the number of standard deviations it sits above or below the mean using `(value − AVG … OVER ()) / STDDEV_SAMP … OVER ()`.

advanced windowing MySQL MariaDB SQL Server PostgreSQL SQLite

Number Rows Within Groups Using ROW_NUMBER

Assign sequential integers to rows within a partition without collapsing the result set the way `GROUP BY` does.

advanced windowing MySQL MariaDB SQL Server PostgreSQL SQLite

Rank Rows With Gaps Using RANK And DENSE_RANK

Assign rank numbers to rows within a partition, controlling whether tied ranks leave gaps in the sequence.

advanced windowing MySQL MariaDB SQL Server PostgreSQL SQLite

Select The Top Row Per Group

Use `ROW_NUMBER()` to rank rows within each group and keep only the highest-ranked row.