SQLite, PostgreSQL, Snowflake and Redis syntax, queries and everyday operations.
Scope and conventions
SQL examples name their database and assume the sample schema where indicated. Redis examples are native redis-cli commands, with optional Search/JSON features labeled. Run independent examples in your own scratch database; driver binding syntax and transaction guarantees differ.
INNER JOIN keeps matching pairs; LEFT JOIN also keeps unmatched left rows with NULL right fields. Multiple matches multiply rows. A right-side predicate in WHERE can discard the NULL-extended rows; place eligibility in ON when unmatched left records must remain.
Fragment
SQLite · Join related records
Follow appearance
✓GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT i.item_id, i.name, c.labelFROM items AS iLEFT JOIN categories AS c ON c.category = i.categoryORDER BY i.item_id;
SQL fragment for the sample items/categories tables in “Create sample records”. Run examples independently.
EXISTS tests whether a subquery returns any row without adding those rows to the outer result. NOT EXISTS expresses an anti-match. A scalar subquery has its own zero/multiple-row behavior; do not assume it is interchangeable with a join.
Fragment
SQLite · Ask whether a related record exists
Follow appearance
✓GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT c.category, c.labelFROM categories AS cWHERE EXISTS ( SELECT 1 FROM items AS i WHERE i.category = c.category AND i.qty > 0)ORDER BY c.category;
SQL fragment for the sample items/categories tables in “Create sample records”. Run examples independently.
WHERE filters input rows; GROUP BY forms groups; HAVING filters summaries. COUNT(*) counts rows; COUNT(column) excludes NULLs. Most aggregates ignore NULL inputs and return NULL for an empty input, while COUNT returns 0. Group by every non-aggregated output expression for portable intent.
Fragment
SQLite · Group, count and filter summaries
Follow appearance
✓GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT category, COUNT(*) AS items, SUM(qty) AS units, AVG(price) AS average_priceFROM itemsWHERE qty >= 0GROUP BY categoryHAVING SUM(qty) > 0ORDER BY category;
SQL fragment for the sample items/categories tables in “Create sample records”. Run examples independently.
DISTINCT removes duplicate result rows. UNION removes duplicates across compatible projections; UNION ALL retains them. INTERSECT returns common rows; EXCEPT removes rows found in the second input. Ordering applies only when explicitly requested for the complete result.
Fragment
SQLite · Handle duplicates and set operations
Follow appearance
✓GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT category FROM itemsUNIONSELECT category FROM categories;SELECT category FROM itemsUNION ALLSELECT category FROM categories;
SQL fragment for the sample items/categories tables in “Create sample records”. Run examples independently.
WITH gives a statement-scoped name to a query result. It improves query structure, but does not by itself promise stored data, a performance improvement or a fixed evaluation strategy. WITH RECURSIVE is the recursive form; define an explicit seed, recurrence and termination policy.
Fragment
SQLite · Name an intermediate query
Follow appearance
✓GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
WITH available AS ( SELECT item_id, name, qty FROM items WHERE qty > 0)SELECT item_id, name FROM availableORDER BY item_id;
SQL fragment for the sample items/categories tables in “Create sample records”. Run examples independently.
A regular view names a query rather than storing a snapshot of its rows. Base-data changes affect later reads. Materialized results have separate refresh and staleness rules. A view alone is not an authorization boundary for a caller that can bypass it.
Fragment
SQLite · Define a reusable query
Follow appearance
✓GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
CREATE VIEW available_items ASSELECT item_id, name, qty FROM items WHERE qty > 0;SELECT * FROM available_items ORDER BY item_id;
SQL fragment for the sample items/categories tables in “Create sample records”. Run examples independently.
OVER defines per-row window context. PARTITION BY divides rows; window ORDER BY determines sequence, independently of final output order. Explicit ROWS frames avoid surprising peer-group defaults for running totals. Filter a computed rank in an outer query where QUALIFY is unavailable.
Fragment
SQLite · Add rank or running context without grouping away rows
Follow appearance
✓GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT item_id, category, qty, ROW_NUMBER() OVER ( PARTITION BY category ORDER BY qty DESC, item_id ) AS position, SUM(qty) OVER ( ORDER BY item_id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS running_unitsFROM itemsORDER BY item_id;
SQL fragment for the sample items/categories tables in “Create sample records”. Run examples independently.
ROW_NUMBER numbers individual rows. RANK gives peers the same rank and leaves gaps afterward; DENSE_RANK does not leave those gaps. Peer identity follows each window’s ORDER BY, not the final output order.
Fragment
SQLite · Distinguish row number, rank and dense rank
Follow appearance
✓GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT item_id, qty, ROW_NUMBER() OVER (ORDER BY qty DESC, item_id) AS position, RANK() OVER (ORDER BY qty DESC) AS rank_with_gaps, DENSE_RANK() OVER (ORDER BY qty DESC) AS rank_without_gapsFROM itemsORDER BY qty DESC, item_id;
Independent SELECT over the sample items table; SQLite 3.25+ window functions.
LAG and LEAD access a previous/next row in the window order. At a missing neighbor, the omitted default is NULL. Define a deterministic order; earlier filtering changes which rows participate.
Fragment
SQLite · Read a previous or next ordered row
Follow appearance
✓GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT item_id, qty, LAG(qty) OVER (ORDER BY item_id) AS previous_qty, LEAD(qty) OVER (ORDER BY item_id) AS next_qtyFROM itemsORDER BY item_id;
Independent SELECT over the sample items table; SQLite 3.25+ window functions.