FIND YOUR NEXT FEW LINES

The snippet library.

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

125 snippets foundClear filters
SQLWindow analytics

Select the top row in each group with a tie-breaker

SQL: Filter an outer query on ROW_NUMBER because window functions are not evaluated in WHERE.

SQL✓ Tested
SQLGetting Started

Select values in an inclusive range

SQL: BETWEEN includes both endpoints of the range.

SQL✓ Tested
SQLWindow analytics

Show a group total beside each detail row

SQL: A partitioned aggregate preserves detail rows instead of collapsing them with GROUP BY.

SQL✓ Tested
SQLSubqueries & ranked selection

Sort preferred rows ahead of other rows

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

SQL✓ Tested
SQLGetting Started

Sort the highest quantities first

SQL: Specify descending order and a stable tie breaker.

SQL✓ Tested
SQLWindow analytics

Sum a row and its immediate predecessor

SQL: A ROWS frame counts physical ordered rows rather than all peers with the same sort value.

SQL✓ Tested
SQLExpressions & aggregates

Sum values per category

Calculate a grouped total.

SUM✓ Tested
SQLChange & combine data

Test missing values with IS NULL

Find rows where an optional value is absent.

IS NULL✓ Tested
SQLText cleanup & matching

Treat an empty trimmed value as missing

SQL: NULLIF converts an empty string to NULL before a fallback is applied.

SQL✓ Tested
SQLGetting Started

Turn a condition into a result label

SQL: Use CASE to describe stock availability for each row.

SQL✓ Tested
SQLReporting & grouped calculations

Turn known group values into columns

SQL: Conditional aggregation gives a portable pivot when the category set is known.

SQL✓ Tested
SQLSchema design & transactions

Undo a multi-row transfer atomically

SQL: The first transaction commits the fixtures. Rolling back the second transaction restores both sides of the attempted transfer.

SQL✓ Tested