sqlcmd.net validated sql reference

#analytics

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

14 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 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 aggregation PostgreSQL

Compute Pearson Correlation and Covariance

Measure the strength of a linear relationship between two columns with `CORR(y, x)` and covariance with `COVAR_SAMP` / `COVAR_POP` in PostgreSQL.

intermediate aggregation MySQL MariaDB SQL Server PostgreSQL SQLite

Compute Standard Deviation and Variance

Measure how spread out values are with `STDDEV_SAMP` / `VAR_SAMP` in MySQL, MariaDB, and PostgreSQL, or `STDEV` / `VAR` in SQL Server.

intermediate aggregation MySQL MariaDB SQL Server PostgreSQL SQLite

Count Users at Each Step of a Conversion Funnel

Measure how many unique users completed each stage of a multi-step flow using `COUNT(DISTINCT CASE WHEN event_name = '...' THEN user_id END)` per step.

advanced aggregation MariaDB SQL Server PostgreSQL

Detect Outliers Using the IQR Method

Flag values that fall outside the interquartile fence `[Q1 − 1.5 × IQR, Q3 + 1.5 × IQR]` using `PERCENTILE_CONT` in PostgreSQL, MariaDB, and SQL Server.

advanced date MySQL MariaDB SQL Server PostgreSQL SQLite

Fill Gaps in a Time Series

Produce a row for every date in a range — even dates with no data — by generating a date spine and left-joining actual records onto it.

intermediate aggregation MySQL MariaDB SQL Server PostgreSQL SQLite

Find the Most Frequent Value (Statistical Mode)

Identify the value that appears most often in a column using `GROUP BY` with `ORDER BY COUNT(*) DESC LIMIT 1`, or with `MODE() WITHIN GROUP` in PostgreSQL.

advanced aggregation PostgreSQL

Fit a Linear Regression With REGR_SLOPE and REGR_INTERCEPT

Compute the least-squares slope, intercept, and R² of a linear regression directly in SQL using `REGR_SLOPE(y, x)`, `REGR_INTERCEPT(y, x)`, and `REGR_R2(y, x)` in PostgreSQL.

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 ()`.