Sql Examples

Page 4 of 7

Browse Sql examples

sqlSnippet

Weighted Average Per Group

Compute a weighted average as SUM(value * weight) over SUM(weight), because averaging already-averaged group values weights small samples the same as big ones.

sqlSnippet

Do Block Loops With Notice

Run anonymous procedural code inline with a DO block, using an integer FOR loop and RAISE NOTICE to emit progress lines from a psql session.

sqlSnippet

Layered CTE Pipeline

Compose a multi-stage query as named CTE layers — filter, deduplicate, then aggregate — so each stage reads independently and later stages stay simple.

sqlSnippet

Pg Prewarm Loads Relation Cache

Load a table or index into shared_buffers with pg_prewarm before benchmarking, so the first measured run does not pay the cold-cache disk read cost.

sqlSnippet

Except Snapshot Row Diff

Diff two row snapshots with EXCEPT in both directions: rows only in the old load are removals, rows only in the new load are additions.

sqlSnippet

Greatest Least Clamp Values

Clamp numbers into bounds with GREATEST and LEAST: chain both for a range clamp; PostgreSQL ignores NULL inputs, returning NULL only when all are NULL.

sqlSnippet

Merge Join Plan Ordering

Feed a Merge Join with pre-sorted inputs: when both sides have matching index order the planner merges streams in one pass without a hash table.