Reusable SQL / Python Snippets Starter Pack
A practical, opinionated starter library of vetted SQL, Python, and R snippets for common analytics tasks (ETL patterns, dedupe, windowing, metric templates, feature engineering, model evaluation) with usage notes, simple tests, and packaging/versioning guidance so teams can adapt, review, and safely reuse code.
Welcome — what this bundle is for
This starter pack collects compact, reusable patterns you can copy, adapt, and review to accelerate cleaning, transformation, metric calculation, sampling, and basic model evaluation. Each snippet includes a brief explanation, recommended inputs/outputs, test ideas, and governance reminders. Use these as starting points — always adapt to your schema, scale, and security requirements.
Contents at a glance
- Standard ETL patterns: incremental load, dedupe, safe upserts
- Date & time window helpers
- Metric calculation templates with simple test cases
- Feature engineering examples (SQL and pandas)
- Model evaluation utilities (sklearn & base R)
- Notebook skeleton, packaging & CI hints
How to use these snippets
- Read the usage notes and assumptions at the top of each snippet.
- Adapt table/column names and parameterize values rather than pasting literals.
- Add a unit test or sample-data check before promoting to shared libraries.
- Record reviewed-and-approved versions in your repo and link them from project docs.
Selected snippets (examples)
1) Incremental load (merge/upsert) — ANSI-style MERGE (SQL)
Assumption: staging table stg_events, target table events, primary key event_id, updated_at timestamp column.
MERGE INTO events AS tgt
USING (SELECT * FROM stg_events) AS src
ON tgt.event_id = src.event_id
WHEN MATCHED AND src.updated_at > tgt.updated_at
THEN UPDATE SET
tgt.user_id = src.user_id,
tgt.event_type = src.event_type,
tgt.updated_at = src.updated_at
WHEN NOT MATCHED
THEN INSERT (event_id, user_id, event_type, created_at, updated_at)
VALUES (src.event_id, src.user_id, src.event_type, src.created_at, src.updated_at);
Notes: Ensure idempotency by relying on updated_at or a change hash. For large volumes, consider partitioned/clustered staging, batched merges, or cloud bulk-load features.
2) Dedupe rows while keeping latest (SQL window)
WITH ranked AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY business_key ORDER BY last_seen DESC) AS rn
FROM source_table
)
SELECT *
FROM ranked
WHERE rn = 1;
Use this to produce a canonical set before upserting. Replace ROW_NUMBER with RANK if duplicates require different tie-breaking.
3) Date & time window helper — sliding window counts (SQL)
SELECT user_id,
event_time,
COUNT(*) OVER (PARTITION BY user_id ORDER BY event_time
RANGE BETWEEN INTERVAL '7' DAY PRECEDING AND CURRENT ROW) AS events_last_7d
FROM events;
Adjust window clause syntax for your SQL dialect (some use ROWS, some use RANGE with intervals).
4) Metric template + test case (SQL)
Example: calculate daily active users (DAU) and a basic test that compares a hand-calculated expected value on a small sample.
-- Metric: DAU (unique users per day)
SELECT date_trunc('day', event_time) AS day,
COUNT(DISTINCT user_id) AS dau
FROM events
WHERE event_time >= '{{start_date}}' AND event_time < '{{end_date}}'
GROUP BY 1
ORDER BY 1;
-- Test idea: run on a test_events table with known rows and assert DAU on sample day = expected value
5) Feature engineering — lag and rolling features in pandas (Python)
import pandas as pd
df = pd.read_parquet('user_events.parquet')
df = df.sort_values(['user_id', 'event_time'])
# last event timestamp per user
last_ts = df.groupby('user_id')['event_time'].last().rename('last_event')
# 7-day event count
cnt_7d = df.set_index('event_time').groupby('user_id')
.rolling('7D').size().reset_index(level=0, drop=True).rename('count_7d')
features = pd.concat([last_ts, cnt_7d.groupby('user_id').last()], axis=1).reset_index()
Tip: generate features on a per-batch basis and hash joins to attach to training data. Keep memory use in mind — use chunking or Dask for larger data.
6) Model evaluation utilities — confusion matrix & common metrics (Python sklearn)
from sklearn.metrics import confusion_matrix, precision_score, recall_score, roc_auc_score
y_pred = model.predict(X_test)
cm = confusion_matrix(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
auc = roc_auc_score(y_test, model.predict_proba(X_test)[:,1])
Wrap these in a small reporting function that returns a dict and a printable summary for notebooks.
7) R: simple model evaluation and tidy output
library(yardstick)
metrics <- metric_set(accuracy, roc_auc, sens, spec)
res <- metrics(data = test_df, truth = truth_col, estimate = .pred_class)
Notebook skeleton & packaging hints
Suggested notebook structure:
- Short purpose statement and inputs/outputs
- Environment + package versions
- Small sample data checks (sanity tests)
- Core transformations in named functions
- Unit test cells or linked pytest/ R testthat files
- Results + simple visualization + next steps
Packaging: place reusable snippets in a repo under /src (Python/R packages) or as SQL files with descriptive names and metadata (author, version, last-reviewed). Include a CHANGELOG and short test harness that runs on sample data in CI.
Testing, governance, and safe reuse
- Always parameterize queries and avoid embedding secrets.
- Add a small automated test or sample-data run that asserts key metric outputs before merging.
- Tag each snippet with assumptions (e.g., "requires updated_at timestamp", "expects partitioned table").
- Track ownership and review status in your repo; require at least one peer review for new shared snippets.
- Document performance expectations and suggested execution plan checks for large-scale use.
Common adaptation checklist
- Map snippet columns to local schema.
- Parameterize dates, partitions, and environment-specific settings.
- Run on representative sample and inspect query plan / memory profile.
- Write a minimal test that will fail fast on misuse.
- Record approvals and link to relevant data governance policies.
Mal-hungers & warnings
Copying snippets without review can create technical debt, inconsistent metrics, performance problems, or data leaks. Use this bundle as a starting point, not a drop-in solution. Enforce reviews, tests, and documentation before publishing in shared libraries.
Next steps — how teams commonly adopt this
- Import the snippets into a shared repository with a clear owner.
- Create a small CI job that runs sample tests for each snippet.
- Hold a short walkthrough with analysts to explain assumptions and safe usage.
- Iterate: add dialect-specific variants (Redshift, BigQuery, Snowflake, Postgres) and record them as separate files.
Where this can grow (ideas)
- Dialect-specific folders + automated test matrix across engines.
- Small executable examples for each snippet (sample data + expected outputs).
- Integration with a snippet manager that surfaces usage metrics and feedback.
Author note: Keep each snippet compact and opinionated. The value lies in clarity of assumptions, testability, and safe reuse.
Discussion
Comments and conversation will live here.