The snippet library.
Small, practical examples. Search by task or narrow things down below.
Find parents whose every child meets a rule
SQLite: Combine existence with the absence of a violating child to exclude parents with no children.
SQLite✓ Tested
Find parents with at least two child rows
SQLite: Use a correlated count when testing a per-parent threshold.
SQLite✓ Tested
Find records without any related rows
SQLite: NOT EXISTS avoids the NULL pitfalls of NOT IN when the subquery column can be missing.
SQLite✓ Tested
Generate every combination of two sets
SQLite: CROSS JOIN creates one result per possible pair, so the output size is the product of the input sizes.
SQLite✓ Tested
Include zero counts for missing relationships
SQLite: Count the non-null child key after a left join so a missing relationship produces zero.
SQLite✓ Tested
Join a parent to pre-aggregated totals
SQLite: Aggregate the child rows first to produce at most one joined row per parent.
SQLite✓ Tested
Join through a lookup table
SQLite: Use explicit aliases and join keys when enriching records through two relationships.
SQLite✓ Tested
Keep parent rows while filtering joined children
SQLite: Put a child filter in ON when unmatched parents must remain in the result.
SQLite✓ Tested
List unordered pairs without duplicate reversals
SQLite: Join using an increasing key comparison to omit self-pairs and reversed duplicates.
SQLite✓ Tested
Pair employees with their managers
SQLite: Alias a table twice to represent two different roles in one relationship.
SQLite✓ Tested