FIND YOUR NEXT FEW LINES

The snippet library.

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

125 snippets foundClear filters
SQL ServerGetting Started

Accept either of two conditions

SQL Server: Use OR to keep matching names or out-of-stock rows.

SQL Server✓ Tested
SQL ServerSchema design & transactions

Add an optional field to an existing table

SQL Server: A nullable column permits existing rows to remain valid while data is filled later.

SQL Server✓ Tested
SQL ServerDates & text

Add days to a date

Use DATEADD for date arithmetic.

DATEADD✓ Tested
SQL ServerGetting Started

Add values across several rows

SQL Server: Use SUM to calculate a total quantity.

SQL Server✓ Tested
SQL ServerConstraints & defaults

Apply a default when an insert omits a column

SQL Server: A default supplies an omitted value; an explicit NULL still follows the column’s nullability rules.

SQL Server✓ Tested
SQL ServerSelect & page

Assign a rank within each group

Number rows with a partitioned window.

ROW_NUMBER✓ Tested
SQL ServerSubqueries & ranked selection

Attach an overall count to every row

SQL Server: An uncorrelated scalar subquery supplies the same aggregate to each output row.

SQL Server✓ Tested
SQL ServerConstraints & defaults

Block deletion of a referenced parent

SQL Server: The default foreign-key action prevents deletion while a child still refers to the parent.

SQL Server✓ Tested
SQL ServerSubqueries & ranked selection

Build a query from several named stages

SQL Server: Separate row selection from aggregation using two common table expressions.

SQL Server✓ Tested
SQL ServerWindow analytics

Calculate a cumulative integer percentage

SQL Server: Divide a running window sum by the complete total using a unique ordering key.

SQL Server✓ Tested
SQL ServerGetting Started

Calculate a total for each group

SQL Server: Group nonmissing categories and calculate separate totals.

SQL Server✓ Tested
SQL ServerGetting Started

Calculate a value with SELECT

SQL Server: Evaluate a small arithmetic expression on the database server.

SQL Server✓ Tested