The snippet library.
Small, practical examples. Search by task or narrow things down below.
Combine results while retaining duplicates
SQL Server: UNION ALL does not spend work removing equal rows.
SQL Server✓ Tested
Copy selected rows into another table
SQL Server: INSERT SELECT performs a set-based copy with an explicit destination column list.
SQL Server✓ Tested
Delete rows selected by a related table
SQL Server: A correlated EXISTS limits the delete to explicitly listed records.
SQL Server✓ Tested
Find rows in the first result but not the second
SQL Server: EXCEPT calculates a distinct result-set difference.
SQL Server✓ Tested
Find rows shared by two query results
SQL Server: INTERSECT compares complete selected rows and removes duplicates.
SQL Server✓ Tested
Increment a stored value in the database
SQL Server: An in-database arithmetic update avoids a separate application read-modify-write step.
SQL Server✓ Tested
Insert only keys absent from a destination
SQL Server: NOT EXISTS supports a set-based missing-row insert in a single-writer example.
SQL Server✓ Tested
Remove duplicate values while keeping the smallest key
SQL Server: Identify retained keys in a grouped subquery before deleting other copies.
SQL Server✓ Tested
Update rows selected by a related table
SQL Server: Use EXISTS to limit updates based on an independent eligibility table.
SQL Server✓ Tested
Update several classifications in one statement
SQL Server: A CASE expression computes the new value for each row covered by the update predicate.
SQL Server✓ Tested