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