FIND YOUR NEXT FEW LINES

The snippet library.

Small, practical examples. Search by task or narrow things down below.

10 snippets foundClear filters
SQL ServerSubqueries & ranked selection

Attach an overall count to every row

SQL Server: An uncorrelated scalar subquery supplies the same aggregate to each output row.

SQL Server✓ Tested
SQL ServerSubqueries & ranked selection

Build a query from several named stages

SQL Server: Separate row selection from aggregation using two common table expressions.

SQL Server✓ Tested
SQL ServerSubqueries & ranked selection

Compare a row with its own group average

SQL Server: A correlated aggregate computes a different comparison value for each group.

SQL Server✓ Tested
SQL ServerSubqueries & ranked selection

Find the second-highest distinct value

SQL Server: Select the maximum below the overall maximum to skip ties at the top.

SQL Server✓ Tested
SQL ServerSubqueries & ranked selection

Include every row tied for the highest score

SQL Server: Compare with MAX rather than limiting to an arbitrary number of rows.

SQL Server✓ Tested
SQL ServerSubqueries & ranked selection

Include missing values in a not-equal filter

SQL Server: Ordinary inequality does not include NULL; explicitly include it when that is the intended rule.

SQL Server✓ Tested
SQL ServerSubqueries & ranked selection

Read a page after a known unique key

SQL Server: A keyset predicate avoids scanning and discarding an increasing offset on a suitable index.

SQL Server✓ Tested
SQL ServerSubqueries & ranked selection

Report duplicate business values and their frequency

SQL Server: Group by a candidate business key and retain only groups with multiple rows.

SQL Server✓ Tested
SQL ServerSubqueries & ranked selection

Return all rows tied for the maximum in a group

SQL Server: An equality comparison with a grouped maximum retains ties rather than choosing one arbitrary row.

SQL Server✓ Tested
SQL ServerSubqueries & ranked selection

Sort preferred rows ahead of other rows

SQL Server: A CASE ordering key expresses business priority before a stable secondary sort.

SQL Server✓ Tested