The snippet library.
Small, practical examples. Search by task or narrow things down below.
Join rows in a chosen order
Aggregate text with STRING_AGG and WITHIN GROUP.
STRING_AGG✓ Tested
Join through a lookup table
SQL Server: Use explicit aliases and join keys when enriching records through two relationships.
SQL Server✓ Tested
Keep children while clearing a deleted relationship
SQL Server: ON DELETE SET NULL requires a nullable foreign-key column.
SQL Server✓ Tested
Keep parent rows while filtering joined children
SQL Server: Put a child filter in ON when unmatched parents must remain in the result.
SQL Server✓ Tested
Keep rows matching a condition
SQL Server: Use WHERE to select rows with a particular category.
SQL Server✓ Tested
List unordered pairs without duplicate reversals
SQL Server: Join using an increasing key comparison to omit self-pairs and reversed duplicates.
SQL Server✓ Tested
Make a pair of columns unique together
SQL Server: A composite primary key permits repeated individual values while forbidding a duplicate combination.
SQL Server✓ Tested
Match a known text prefix
SQL Server: A percent wildcard matches zero or more characters following the prefix.
SQL Server✓ Tested
Match a text prefix
SQL Server: Use a percent wildcard after a known prefix.
SQL Server✓ Tested
Match one value from a short list
SQL Server: Use IN for a set of allowed values.
SQL Server✓ Tested
Measure the length of ordinary text
SQL Server: Use the engine’s text-length function; this ASCII example has no trailing spaces.
SQL Server✓ Tested
Normalize ASCII text to lowercase
SQL Server: LOWER is useful for display normalization; indexed case-insensitive search depends on the database collation and schema.
SQL Server✓ Tested