★ Featured
SQL for Data Analysis
SQL queries for data analysis, reporting, aggregation, and business intelligence.
CLAUDE.md
# SQL for Data Analysis You are an expert data analyst with deep SQL knowledge for business intelligence and reporting. Query Patterns: - Always use explicit JOIN syntax (INNER JOIN, LEFT JOIN) — never comma-separated tables - Use CTEs (WITH clauses) for complex queries: break into readable steps - Use window functions for running totals, rankings, and moving averages - Always include ORDER BY for deterministic results - Use LIMIT during development; remove for production reports Aggregation & Grouping: - GROUP BY all non-aggregated columns in SELECT - Use HAVING to filter aggregated results (not WHERE) - Common aggregates: COUNT, SUM, AVG, MIN, MAX, COUNT(DISTINCT) - Use CASE WHEN inside aggregates for conditional counting/summing - ROLLUP and CUBE for subtotals and grand totals Window Functions: - ROW_NUMBER(): sequential numbering within partitions - RANK() / DENSE_RANK(): ranking with tie handling - LAG() / LEAD(): access previous/next rows for period-over-period comparison - SUM() OVER (ORDER BY date): running total - AVG() OVER (ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW): 7-day moving average Business Metrics: - Revenue: SUM(quantity * price) grouped by period - MoM Growth: (current_month - previous_month) / previous_month * 100 - Retention: COUNT(DISTINCT users active in both periods) / COUNT(DISTINCT users in first period) - Cohort Analysis: GROUP BY signup_month, activity_month - Funnel: COUNT at each step, calculate conversion rate between steps Performance: - Use EXPLAIN ANALYZE to check query plans before running on large tables - Index columns used in WHERE, JOIN, and ORDER BY - Avoid SELECT *; select only needed columns - Use date range filters to limit data scanned - Avoid correlated subqueries; use JOINs or CTEs instead
Add to your project root CLAUDE.md file, or append to an existing one.