← Back to all products
$29
Advanced SQL Query Library
200+ production SQL queries organized by use case: cohort analysis, funnel metrics, retention, revenue, and operational reporting.
SQLMarkdownPostgreSQL
📄 Product Preview
Try the interactive reader and demo tools below, or get the full product with all content unlocked.
📖 Interactive Reader (Free Preview) ⚙ Try Demo Tools 📦 Download Free Sample📁 File Structure 72 files
sql-query-library/
├── INDEX.md
├── LICENSE
├── README.md
├── cohort-analysis/
│ ├── 01_weekly_signup_cohorts.sql
│ ├── 02_behavioral_cohorts.sql
│ ├── 03_cohort_revenue_matrix.sql
│ ├── 04_cohort_size_decay.sql
│ ├── 05_acquisition_channel_cohorts.sql
│ ├── 06_plan_upgrade_cohorts.sql
│ ├── 07_geographic_cohorts.sql
│ └── 08_lifecycle_stage_classification.sql
├── free-sample.zip
├── funnel-metrics/
│ ├── 01_basic_conversion_funnel.sql
│ ├── 02_time_between_steps.sql
│ ├── 03_funnel_dropoff_by_segment.sql
│ ├── 04_funnel_over_time.sql
│ ├── 05_parallel_funnels.sql
│ ├── 06_funnel_reentry_analysis.sql
│ ├── 07_device_split_funnel.sql
│ └── 08_funnel_velocity.sql
├── guide/
│ └── query-optimization.md
├── guides/
│ └── query-optimization.md
├── index.html
├── marketing/
│ ├── 01_channel_attribution.sql
│ ├── 02_multi_touch_attribution.sql
│ ├── 03_campaign_roi.sql
│ ├── 04_cac_payback_period.sql
│ ├── 05_channel_efficiency.sql
│ ├── 06_email_campaign_metrics.sql
│ ├── 07_organic_vs_paid.sql
│ └── 08_referral_network.sql
├── operational/
│ ├── 01_sla_compliance.sql
│ ├── 02_queue_depth_trends.sql
│ ├── 03_first_response_time.sql
│ ├── 04_agent_throughput.sql
│ ├── 05_peak_load_analysis.sql
│ ├── 06_resolution_time_by_category.sql
│ ├── 07_escalation_patterns.sql
│ └── 08_satisfaction_drivers.sql
├── product-analytics/
│ ├── 01_feature_adoption_rates.sql
│ ├── 02_engagement_scoring.sql
│ ├── 03_power_user_identification.sql
│ ├── 04_feature_usage_frequency.sql
│ ├── 05_session_depth_analysis.sql
│ ├── 06_aha_moment_detection.sql
│ ├── 07_feature_interaction_paths.sql
│ └── 08_stickiness_ratio.sql
├── retention/
│ ├── 01_classic_day_n_retention.sql
│ ├── 02_rolling_retention.sql
│ ├── 03_retention_by_first_action.sql
│ ├── 04_weekly_retention_curve.sql
│ ├── 05_resurrection_rate.sql
│ ├── 06_feature_retention_correlation.sql
│ ├── 07_revenue_retention.sql
│ └── 08_churn_prediction_features.sql
├── revenue/
│ ├── 01_monthly_mrr_waterfall.sql
│ ├── 02_ltv_by_cohort.sql
│ ├── 03_arpu_trends.sql
│ ├── 04_expansion_contraction.sql
│ ├── 05_payment_failure_analysis.sql
│ ├── 06_discount_impact.sql
│ ├── 07_revenue_concentration.sql
│ └── 08_arr_forecasting_inputs.sql
├── schema/
│ └── sample_schema.sql
└── window-functions/
├── 01_running_totals.sql
├── 02_moving_averages.sql
├── 03_rank_and_percentile.sql
├── 04_sessionization.sql
├── 05_lead_lag_comparisons.sql
├── 06_first_last_per_group.sql
├── 07_gap_and_island_detection.sql
└── 08_cumulative_distribution.sql
📖 Documentation Preview README excerpt
Advanced SQL Query Library
A curated collection of 60+ production-tested SQL queries for data analysts. Every query is documented, parameterized, and annotated with dialect-specific notes for PostgreSQL, BigQuery, and Snowflake.
What's Inside
- 200+ hours of query development distilled into copy-paste-ready patterns
- 8 category folders covering the full analytics workflow
- Dialect annotations — each query notes where syntax differs across Postgres, BigQuery, and Snowflake
- Parameterized — swap in your table/column names and run immediately
- Sample schema (DDL) so you can test every query locally
Categories
| Folder | Focus | Query Count |
|---|---|---|
cohort-analysis/ | User cohorts, behavioral segmentation, lifecycle stages | 8 |
funnel-metrics/ | Conversion funnels, drop-off analysis, step timing | 8 |
retention/ | Day-N retention, rolling retention, churn prediction inputs | 8 |
revenue/ | MRR/ARR, LTV, expansion/contraction, payment analytics | 8 |
operational/ | SLA monitoring, queue depth, throughput, capacity planning | 8 |
marketing/ | Attribution, campaign ROI, channel performance, CAC | 8 |
product-analytics/ | Feature adoption, engagement scoring, power users | 8 |
window-functions/ | Ranking, running totals, sessionization, gap analysis | 8 |
Quick Start
1. Load the sample schema into your database:
\i schema/sample_schema.sql -- Postgres
2. Browse INDEX.md to find the query you need.
3. Open the .sql file, read the header comment, replace parameters (marked {{parameter_name}}), and run.
Query File Format
Every .sql file follows this structure:
-- ============================================================
-- Query: <descriptive name>
-- Category: <folder name>
-- Dialect: ANSI SQL (notes below for Postgres/BQ/Snowflake)
-- Parameters:
-- {{start_date}} — Beginning of analysis window (DATE)
-- {{end_date}} — End of analysis window (DATE)
-- Description:
-- <What this query computes and when to use it>
-- ============================================================
SELECT ...
-- DIALECT NOTES:
-- Postgres: <any Postgres-specific syntax>
-- BigQuery: <any BigQuery-specific syntax>
-- Snowflake: <any Snowflake-specific syntax>
... continues with setup instructions, usage examples, and more.
📄 Code Sample .sql preview
cohort-analysis/01_weekly_signup_cohorts.sql
-- ============================================================
-- Query: Weekly Signup Cohorts
-- Category: cohort-analysis
-- Dialect: ANSI SQL (notes below for Postgres/BQ/Snowflake)
-- Parameters:
-- {{start_date}} — Cohort start date (DATE, e.g. '2024-01-01')
-- {{end_date}} — Cohort end date (DATE, e.g. '2024-06-30')
-- {{activity_event}} — Event name that counts as "active" (VARCHAR)
-- Description:
-- Groups users into weekly signup cohorts and measures how many
-- remain active in each subsequent week. This is the foundation
-- of most retention analysis — understand when users drop off
-- by looking at weekly cohort curves.
-- ============================================================
WITH cohort_assignment AS (
-- Assign each user to their signup week cohort
SELECT
user_id,
DATE_TRUNC('week', signup_date) AS cohort_week,
signup_date
FROM users
WHERE signup_date BETWEEN {{start_date}} AND {{end_date}}
),
user_activity AS (
-- Get weekly activity for each user (distinct weeks they were active)
SELECT
e.user_id,
DATE_TRUNC('week', e.event_timestamp) AS activity_week
FROM events e
INNER JOIN cohort_assignment ca ON e.user_id = ca.user_id
WHERE e.event_name = {{activity_event}}
AND e.event_timestamp >= ca.signup_date
GROUP BY e.user_id, DATE_TRUNC('week', e.event_timestamp)
),
cohort_activity AS (
-- Calculate weeks since signup for each activity record
SELECT
ca.cohort_week,
ua.activity_week,
-- Weeks elapsed since cohort start
EXTRACT(DAY FROM ua.activity_week - ca.cohort_week) / 7 AS weeks_since_signup,
ca.user_id
FROM cohort_assignment ca
INNER JOIN user_activity ua ON ca.user_id = ua.user_id
)
SELECT
# ... 21 more lines ...