Sql Analysis
by spjoshis
Master SQL for data analysis with complex queries, joins, aggregations, window functions, and query optimization.
Skill Details
Repository Files
1 file in this skill directory
name: sql-analysis description: Master SQL for data analysis with complex queries, joins, aggregations, window functions, and query optimization.
SQL Analysis
Master SQL for extracting, transforming, and analyzing data using complex queries, joins, aggregations, and advanced SQL techniques.
When to Use This Skill
- Data extraction
- Business reporting
- Ad-hoc analysis
- Data exploration
- Metric calculation
- Customer segmentation
- Funnel analysis
- Cohort analysis
Core Concepts
1. Complex Joins
-- Customer purchase analysis with multiple joins
SELECT
c.customer_id,
c.name,
COUNT(DISTINCT o.order_id) as total_orders,
SUM(oi.quantity * oi.price) as total_revenue,
AVG(o.order_total) as avg_order_value
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
LEFT JOIN order_items oi ON o.order_id = oi.order_id
WHERE o.order_date >= '2024-01-01'
GROUP BY c.customer_id, c.name
HAVING COUNT(DISTINCT o.order_id) >= 3
ORDER BY total_revenue DESC;
2. Window Functions
-- Monthly revenue with running total and growth
SELECT
DATE_TRUNC('month', order_date) as month,
SUM(order_total) as monthly_revenue,
SUM(SUM(order_total)) OVER (
ORDER BY DATE_TRUNC('month', order_date)
) as running_total,
LAG(SUM(order_total)) OVER (
ORDER BY DATE_TRUNC('month', order_date)
) as prev_month_revenue,
ROUND(
(SUM(order_total) - LAG(SUM(order_total)) OVER (ORDER BY DATE_TRUNC('month', order_date)))
/ LAG(SUM(order_total)) OVER (ORDER BY DATE_TRUNC('month', order_date)) * 100,
2
) as growth_pct
FROM orders
GROUP BY DATE_TRUNC('month', order_date)
ORDER BY month;
3. CTEs (Common Table Expressions)
-- Customer cohort retention analysis
WITH first_purchase AS (
SELECT
customer_id,
MIN(order_date) as cohort_month
FROM orders
GROUP BY customer_id
),
monthly_activity AS (
SELECT
fp.customer_id,
fp.cohort_month,
DATE_TRUNC('month', o.order_date) as activity_month,
EXTRACT(MONTH FROM AGE(o.order_date, fp.cohort_month)) as months_since_first
FROM first_purchase fp
JOIN orders o ON fp.customer_id = o.customer_id
)
SELECT
cohort_month,
months_since_first,
COUNT(DISTINCT customer_id) as active_customers
FROM monthly_activity
GROUP BY cohort_month, months_since_first
ORDER BY cohort_month, months_since_first;
Best Practices
- Use CTEs - Readable, maintainable complex queries
- Index aware - Understand query performance
- **Avoid SELECT *** - Specify needed columns
- Comment queries - Explain business logic
- Test incrementally - Build queries step by step
- Handle NULLs - Use COALESCE, proper joins
- Aggregate before join - Reduce data volume
- Use EXPLAIN - Analyze query plans
Resources
- Mode SQL Tutorial: https://mode.com/sql-tutorial/
- SQL Style Guide: https://www.sqlstyle.guide/
Related Skills
Xlsx
Comprehensive spreadsheet creation, editing, and analysis with support for formulas, formatting, data analysis, and visualization. When Claude needs to work with spreadsheets (.xlsx, .xlsm, .csv, .tsv, etc) for: (1) Creating new spreadsheets with formulas and formatting, (2) Reading or analyzing data, (3) Modify existing spreadsheets while preserving formulas, (4) Data analysis and visualization in spreadsheets, or (5) Recalculating formulas
Clickhouse Io
ClickHouse database patterns, query optimization, analytics, and data engineering best practices for high-performance analytical workloads.
Clickhouse Io
ClickHouse database patterns, query optimization, analytics, and data engineering best practices for high-performance analytical workloads.
Analyzing Financial Statements
This skill calculates key financial ratios and metrics from financial statement data for investment analysis
Data Storytelling
Transform data into compelling narratives using visualization, context, and persuasive structure. Use when presenting analytics to stakeholders, creating data reports, or building executive presentations.
Kpi Dashboard Design
Design effective KPI dashboards with metrics selection, visualization best practices, and real-time monitoring patterns. Use when building business dashboards, selecting metrics, or designing data visualization layouts.
Dbt Transformation Patterns
Master dbt (data build tool) for analytics engineering with model organization, testing, documentation, and incremental strategies. Use when building data transformations, creating data models, or implementing analytics engineering best practices.
Sql Optimization Patterns
Master SQL query optimization, indexing strategies, and EXPLAIN analysis to dramatically improve database performance and eliminate slow queries. Use when debugging slow queries, designing database schemas, or optimizing application performance.
Anndata
This skill should be used when working with annotated data matrices in Python, particularly for single-cell genomics analysis, managing experimental measurements with metadata, or handling large-scale biological datasets. Use when tasks involve AnnData objects, h5ad files, single-cell RNA-seq data, or integration with scanpy/scverse tools.
Xlsx
Spreadsheet toolkit (.xlsx/.csv). Create/edit with formulas/formatting, analyze data, visualization, recalculate formulas, for spreadsheet processing and analysis.
