FIND YOUR NEXT FEW LINES

The snippet library.

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

125 snippets foundClear filters
SQL ServerSchema design & transactions

Enforce uniqueness with a dedicated index

SQL Server: A unique index rejects a duplicate combination independently of query plans.

SQL Server✓ Tested
SQL ServerSchema design & transactions

Expose a reusable filtered view

SQL Server: A view stores a query definition rather than a snapshot of its rows.

SQL Server✓ Tested
SQL ServerGetting Started

Filter by a numeric comparison

SQL Server: Keep rows whose quantity exceeds a chosen threshold.

SQL Server✓ Tested
SQL ServerGetting Started

Filter groups after aggregation

SQL Server: Use HAVING to test a group’s calculated total.

SQL Server✓ Tested
SQL ServerReporting & grouped calculations

Filter rows before grouping

SQL Server: WHERE limits the rows contributing to the aggregate; it differs from filtering completed groups with HAVING.

SQL Server✓ Tested
SQL ServerGetting Started

Find missing values with IS NULL

SQL Server: Use IS NULL rather than equality to test for missing data.

SQL Server✓ Tested
SQL ServerRelationships & missing data

Find parents whose every child meets a rule

SQL Server: Combine existence with the absence of a violating child to exclude parents with no children.

SQL Server✓ Tested
SQL ServerRelationships & missing data

Find parents with at least two child rows

SQL Server: Use a correlated count when testing a per-parent threshold.

SQL Server✓ Tested
SQL ServerRelationships & missing data

Find records without any related rows

SQL Server: NOT EXISTS avoids the NULL pitfalls of NOT IN when the subquery column can be missing.

SQL Server✓ Tested
SQL ServerBulk changes & set operations

Find rows in the first result but not the second

SQL Server: EXCEPT calculates a distinct result-set difference.

SQL Server✓ Tested
SQL ServerBulk changes & set operations

Find rows shared by two query results

SQL Server: INTERSECT compares complete selected rows and removes duplicates.

SQL Server✓ Tested
SQL ServerDates & text

Find the final day of a month

Use EOMONTH without manually calculating month length.

EOMONTH✓ Tested