Sql Examples

Page 5 of 7

Browse Sql examples

sqlSnippet

COLLATE Explicit Sort Order

Control string comparison per column, per query, and per index with COLLATE, so the same names sort differently per language and indexes stay usable.

sqlSnippet

COPY FROM STDIN CSV Bulk Load

Load CSV in one atomic statement with COPY, choosing server-side file reads or the client-side \copy equivalent and skipping row-by-row inserts.

sqlSnippet

Cross Join Combination Matrix

Build a full combination matrix with CROSS JOIN, then LEFT JOIN real facts onto the skeleton so empty combinations show as zero instead of disappearing.

sqlSnippet

Drop Column Is Metadata Only

ALTER TABLE DROP COLUMN finishes instantly regardless of table size because only catalog metadata flips; old bytes stay in the heap until a rewrite.

sqlSnippet

For Update Nowait Fail Fast

FOR UPDATE NOWAIT fails immediately with lock_not_available when another transaction holds the row, choosing a loud failure over unbounded queue waiting.

sqlSnippet

Index Cond Versus Filter

Index Cond pushes equality and range predicates into the index scan; non-indexed columns surface as a post-scan Filter with rows-removed cost.

sqlSnippet

Row Number Ties Need Tiebreak

ROW_NUMBER assigns arbitrary order among rows tied on the sort key; add a unique tiebreaker to make numbering, paging, and deduplication reproducible.

sqlSnippet

Sessionize Clicks With Lag

Split event streams into sessions: LAG measures the gap since the previous click, a flag marks 30-minute gaps, and a running SUM assigns session numbers.

sqlSnippet

String Agg Cumulative Window

Build running event trails with string_agg as a window aggregate: ROWS frames accumulate from the partition start or a trailing window of recent steps.

sqlSnippet

Update Top N Per Group Via CTE

Window functions cannot appear in an UPDATE's WHERE clause, so rank rows in a CTE with ROW_NUMBER and flip status through UPDATE FROM past the newest order.