Reusable SQL, Python & R Snippets Index

A curated, practical library of vetted SQL, Python, and R snippets for common data tasks (cleaning, validation, transformation, aggregation, sampling, and reporting). Each snippet includes usage notes, inputs/outputs, performance cautions, and adaptation guidance so analysts produce reliable, reproducible results faster.

Welcome

This repository is a living index of reusable SQL, Python, and R patterns you can adapt for common data tasks: data quality checks, rolling aggregates, cohort queries, time-windowed joins, standard visualizations, forecasting scaffolds, sampling, and reporting templates. The goal is to save analyst time while encouraging review, testing, and local adaptation so snippets become reliable building blocks rather than copy-paste hazards.

How to use this index

  1. Find a category or example that matches your task.
  2. Read the "When to use" and "Inputs / Outputs" carefully.
  3. Adapt table/field names, parameterize literals, and test on a representative dataset before production use.
  4. Follow the review checklist (Governance section) before sharing or embedding into reports, dashboards, or pipelines.

Repository Conventions

  • Language: Each snippet is tagged SQL / Python / R.
  • Parameters: Use clear placeholder notation such as <schema.table>, <start_date>, <granularity> or :param for parameterized clients.
  • Inputs / Outputs: Each snippet lists expected inputs and sample outputs (columns and types).
  • Performance Notes: Indexing, joins, and window specs are called out where relevant.
  • Security & Governance: Snippets should avoid leaking PII and must follow access-control rules in your environment.

Category Index (examples)

1. Data quality checks

Quick checks to validate row counts, nulls, referential integrity, and distribution sanity.

Example: Null / unexpected value counts (SQL)

When to use: initial data profiling or monitoring after an ETL run.

Inputs: <schema.table>, list of columns to check.

Outputs: counts per column of nulls and excluded values.

SELECT
  '{column}' AS column_name,
  COUNT(*) FILTER (WHERE {column} IS NULL) AS null_count,
  COUNT(*) FILTER (WHERE {column} = '') AS empty_string_count
FROM <schema.table>;

Notes: Replace {column} with each column or unnest with a column list; prefer parameterized jobs over string substitution.

2. Rolling aggregates and windowing

Common rolling-sum, moving-average, and lag/lead operations used in time-series funnels and operational metrics.

Example: 7-day rolling average (SQL)

Inputs: date, metric column, partition keys (optional).

SELECT
  date,
  metric,
  AVG(metric) OVER (PARTITION BY {partition_cols} ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS ma_7
FROM <schema.table>
WHERE date BETWEEN :start_date AND :end_date;

Notes: Use appropriate ORDER BY column with consistent granularity; watch out for gaps — consider time series calendar joins.

3. Cohort queries

Segment users by join/install/purchase date and measure retention/behavior across windows.

Example: Monthly cohort retention scaffold (SQL)

WITH cohorts AS (
  SELECT user_id, DATE_TRUNC('month', signup_date) AS cohort_month
  FROM <schema.users>
), events AS (
  SELECT user_id, DATE_TRUNC('month', event_date) AS event_month
  FROM <schema.events>
)
SELECT
  c.cohort_month,
  e.event_month,
  COUNT(DISTINCT e.user_id) AS active_users
FROM cohorts c
JOIN events e USING (user_id)
GROUP BY 1,2
ORDER BY 1,2;

Notes: Use DISTINCT on user_id when counting active users; pivot or visualization is commonly applied after this query.

4. Time-windowed joins

Patterns for joining event streams to state tables with time constraints (e.g., last-known state before event).

5. Standard visualizations (Python / R)

Reusable plotting scaffolds for consistent visuals (matplotlib/ggplot templates) including labeling conventions and color palettes aligned to accessibility.

Example: Python Matplotlib Time Series Template

def plot_time_series(df, date_col='date', value_col='value', title='', ax=None):
    import matplotlib.pyplot as plt
    if ax is None:
        fig, ax = plt.subplots(figsize=(10,4))
    ax.plot(df[date_col], df[value_col], marker='o')
    ax.set_title(title)
    ax.set_xlabel('Date')
    ax.set_ylabel(value_col)
    ax.grid(True)
    return ax

Notes: Accepts a tidy dataframe; suitable for use in notebooks and reporting pipelines.

6. Forecasting scaffolds

Starter patterns to prepare data and run lightweight forecasting models (ARIMA, ETS, or a simple FB Prophet/Neural approach). Emphasize train/test splits and backtesting.

Snippet Template (recommended)

Each snippet entry should include these fields so others can evaluate and adapt it quickly:

  1. Title
  2. Language / Runtime
  3. Purpose / When to use
  4. Inputs (tables, columns, parameter names)
  5. Outputs (columns and types)
  6. Code (with placeholders)
  7. Performance / Indexing notes
  8. Security / Privacy cautions
  9. Test cases and sample data
  10. Version / Author / Last-reviewed

Governance & Review Checklist

  • Does the snippet avoid embedding credentials or returning PII? (If not, redact.)
  • Are table and column names parameterized or clearly documented before use?
  • Has the snippet been run on a small representative dataset and performance observed?
  • Are edge cases (NULLs, duplicates, time zone issues) considered in the notes?
  • Is there an owner and version tag so teams can propose updates?

Contributing & Next Steps

To add a snippet, follow the snippet template above and include tests or sample data where possible. Consider adding a short demonstration notebook for Python/R snippets.

Suggested enhancements for the platform (not yet implemented here): an interactive submission form to capture snippet metadata and code, stored submissions via the content submission endpoint, and an approval workflow so teams can review, tag, and publish reusable snippets.

Final notes

These snippets are starting points — they save time but require adaptation, review, and testing in your environment. Treat this index as a lab: experiment thoughtfully, document changes, and help the community by contributing improved patterns back to the collection.


Discussion

Comments and conversation will live here.