Sql Examples

Page 3 of 7

Browse Sql examples

sqlSnippet

For Update Of Targets One Table

FOR UPDATE OF o locks only the listed table rows in a join; without OF every participating table gets row locks, including pure filter-side tables.

sqlSnippet

INITCAP TRANSLATE Text Cleanup

Normalize messy contact strings with INITCAP for word-initial capitalization and TRANSLATE for per-character masking or deletion via an empty replacement set.

sqlSnippet

Regexp Split To Table Rows

Explode free-form tag lists into rows with regexp_split_to_table behind a LATERAL join; a [,;\s]+ delimiter absorbs the mixed separators split_part preserves.

sqlSnippet

String To Array Token Roundtrip

Tokenize a delimited column with string_to_array, filter with = ANY, and rebuild text with array_to_string, where a null string turns empty fields into NULL.

sqlSnippet

WindowAgg Plan Sort And Index

Read EXPLAIN for window queries: the Sort node feeding WindowAgg, how a matching index removes the Sort, and why distinct windows stack separate nodes.

sqlSnippet

GiST KNN Orders By Distance

GiST indexes answer nearest-neighbor queries directly: ORDER BY location <-> point returns rows from nearest to farthest without a sort step.

sqlSnippet

UNION Stops Recursive CTE Loops

Cycle-tainted hierarchy walks terminate when the recursive CTE uses UNION: duplicate rows are discarded, so a repeated node produces nothing new.

sqlSnippet

VALUES CTE Inline Lookup Join

Turn a small hand-written tuple list into an inline WITH ... AS (VALUES ...) CTE and LEFT JOIN against it, so unmatched parameters return zero rows.

sqlSnippet

COUNT Star Versus COUNT Column

COUNT(*) counts every row while COUNT(col) skips NULLs — so they disagree on sparse columns, and COUNT(DISTINCT col) collapses repeats before skipping NULLs.

sqlSnippet

SELECT FOR SHARE Read Locking

FOR SHARE takes a row lock that blocks writers but permits other readers, unlike the plain snapshot read that lets a concurrent UPDATE slip between checks.