FIND YOUR NEXT FEW LINES

The snippet library.

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

125 snippets foundClear filters
PostgresSubqueries & ranked selection

Read a page after a known unique key

Postgres: A keyset predicate avoids scanning and discarding an increasing offset on a suitable index.

Postgres✓ Tested
PostgresGetting Started

Read named columns from a table

Postgres: Choose the columns needed and request a deterministic row order.

Postgres✓ Tested
PostgresWindow analytics

Read the last value of the complete partition

Postgres: Specify an unbounded-following frame so LAST_VALUE does not stop at the current row.

Postgres✓ Tested
PostgresWindow analytics

Read the next row’s value

Postgres: LEAD reads a later ordered row without a self join and supports a fallback at the end.

Postgres✓ Tested
PostgresGetting Started

Read the smallest and largest values

Postgres: Use MIN and MAX without sorting and fetching every row.

Postgres✓ Tested
PostgresSchema design & transactions

Read updated base-table values through a view

Postgres: An ordinary view reflects later changes to its underlying data.

Postgres✓ Tested
PostgresConstraints & defaults

Reference a parent identified by two columns

Postgres: A composite foreign key checks the complete referenced key rather than either column independently.

Postgres✓ Tested
PostgresConstraints & defaults

Reject a negative quantity with CHECK

Postgres: Combine NOT NULL and CHECK because an unknown NULL check result is normally accepted.

Postgres✓ Tested
PostgresConstraints & defaults

Reject duplicate business identifiers

Postgres: A UNIQUE constraint protects a business key independently of the primary key.

Postgres✓ Tested
PostgresConstraints & defaults

Remove dependent rows with a declared cascade

Postgres: ON DELETE CASCADE removes children when their referenced parent is deleted.

Postgres✓ Tested
PostgresGetting Started

Remove duplicate result values

Postgres: Use DISTINCT when repeated category values are not useful.

Postgres✓ Tested
PostgresBulk changes & set operations

Remove duplicate values while keeping the smallest key

Postgres: Identify retained keys in a grouped subquery before deleting other copies.

Postgres✓ Tested