The snippet library.
Small, practical examples. Search by task or narrow things down below.
Enforce uniqueness with a dedicated index
Postgres: A unique index rejects a duplicate combination independently of query plans.
Postgres✓ Tested
Expand an array into rows
Use WITH ORDINALITY to retain original element order.
unnest✓ Tested
Expose a reusable filtered view
Postgres: A view stores a query definition rather than a snapshot of its rows.
Postgres✓ Tested
Extract part of a date
Read a calendar component with EXTRACT.
EXTRACT✓ Tested
Filter by a numeric comparison
Postgres: Keep rows whose quantity exceeds a chosen threshold.
Postgres✓ Tested
Filter groups after aggregation
Postgres: Use HAVING to test a group’s calculated total.
Postgres✓ Tested
Filter rows before grouping
Postgres: WHERE limits the rows contributing to the aggregate; it differs from filtering completed groups with HAVING.
Postgres✓ Tested
Find missing values with IS NULL
Postgres: Use IS NULL rather than equality to test for missing data.
Postgres✓ Tested
Find parents whose every child meets a rule
Postgres: Combine existence with the absence of a violating child to exclude parents with no children.
Postgres✓ Tested
Find parents with at least two child rows
Postgres: Use a correlated count when testing a per-parent threshold.
Postgres✓ Tested
Find records without any related rows
Postgres: NOT EXISTS avoids the NULL pitfalls of NOT IN when the subquery column can be missing.
Postgres✓ Tested
Find rows in the first result but not the second
Postgres: EXCEPT calculates a distinct result-set difference.
Postgres✓ Tested