The snippet library.
Small, practical examples. Search by task or narrow things down below.
Accept either of two conditions
Postgres: Use OR to keep matching names or out-of-stock rows.
Postgres✓ Tested
Add values across several rows
Postgres: Use SUM to calculate a total quantity.
Postgres✓ Tested
Calculate a total for each group
Postgres: Group nonmissing categories and calculate separate totals.
Postgres✓ Tested
Calculate a value with SELECT
Postgres: Evaluate a small arithmetic expression on the database server.
Postgres✓ Tested
Count the rows in a table
Postgres: COUNT(*) includes every row, including rows with NULL columns.
Postgres✓ Tested
Create a table and insert its first row
Postgres: Define column types and a primary key, then insert one row using an explicit column list.
Postgres✓ Tested
Delete one row from a practice table
Postgres: Restrict DELETE with a primary-key condition and verify the remaining row count.
Postgres✓ Tested
Display a fallback for missing data
Postgres: Return a supplied label when a column contains NULL.
Postgres✓ Tested
Filter by a numeric comparison
Postgres: Keep rows whose quantity exceeds a chosen threshold.
Postgres✓ Tested
Filter groups after aggregation
Postgres: Use HAVING to test a group’s calculated total.
Postgres✓ Tested
Find missing values with IS NULL
Postgres: Use IS NULL rather than equality to test for missing data.
Postgres✓ Tested
Give a result column a readable name
Postgres: Use AS to label a computed result column.
Postgres✓ Tested