Skill

Build Complex SQL Queries for BI and Analytics

A skill writing complex, performance-optimized SQL: window functions, cohort analysis, and incremental batching.


79
Spark score
out of 100
Updated 7 months ago
Version 1.0.0
Models

Add to Favorites

Why it matters

Generate sophisticated and optimized SQL queries for business intelligence, data analysis, and advanced database operations. This asset excels at creating efficient, readable, and scalable queries for complex business logic, joins, and analytical functions.

Outcomes

What it gets done

01

Design and write SQL queries with clarity, readability, and performance in mind.

02

Implement advanced query patterns including window functions and complex aggregations.

03

Optimize queries for performance using techniques like efficient joins and subqueries.

04

Develop robust queries for data validation and error handling.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/vb-complex-sql-query-builder | bash

Overview

Complex SQL Query Builder Agent

This skill writes advanced SQL - window-function running totals and rankings, percentile-based customer tiering, cohort retention analysis, incremental batch processing, and pre-validated data quality checks - optimized for performance from the start. Use it when a query needs advanced analytical logic built for correctness and performance, not just a working SELECT statement.

What it does

This skill is an expert in designing complex SQL for business intelligence, data analysis, and advanced database operations - efficient, maintainable queries handling intricate business logic, multiple joins, analytical functions, and performance optimization across database systems. Core design principles: clarity and readability (self-documenting SQL, meaningful aliases, CTEs to decompose complex logic), performance first (execution plans, indexing, EXISTS vs IN, join order, temp tables), scalability (filtering strategy, avoiding unnecessary sorts, partitioning), and maintainability (parameterization, no hardcoded values, consistent naming).

When to use - and when NOT to

Use it when a query needs advanced analytical logic - running totals, cohort retention, multi-dimensional aggregation - built for correctness and performance from the start, not just a working SELECT statement.

WITH sales_analysis AS (
    SELECT
        region,
        sales_date,
        revenue,
        SUM(revenue) OVER (
            PARTITION BY region
            ORDER BY sales_date
            ROWS UNBOUNDED PRECEDING
        ) AS running_total,
        RANK() OVER (
            PARTITION BY region, DATE_TRUNC('month', sales_date)
            ORDER BY revenue DESC
        ) AS monthly_rank
    FROM sales_data
    WHERE sales_date >= '2024-01-01'
)
SELECT * FROM sales_analysis;

Inputs and outputs

Beyond window functions, it covers multi-level customer aggregation with percentile-based value tiering (PERCENTILE_CONT for P75/P25 average-order-value benchmarks) and lifecycle staging (At Risk / Active / Dormant by recency), optimized EXISTS-based filtering over large joined datasets with matching index recommendations, an incremental batch-processing pattern using generate_series to chunk large date ranges with a running cumulative total, a full customer cohort retention analysis (first-purchase month, activity by period number, retention rate per cohort), and a data-quality pattern that classifies and quarantines invalid rows (non-positive values, future dates, missing keys) before the main analysis runs only against validated records.

Integrations

Performance guidance names database-specific features to reach for: LATERAL joins in PostgreSQL, APPLY operators in SQL Server, and optimizer hints in MySQL, alongside general practices like EXPLAIN ANALYZE, cursors or paginated retrieval for large result sets, and LIMIT clauses during development.

Who it's for

Data analysts and engineers writing BI or reporting SQL who need advanced, production-grade patterns - window functions, cohort analysis, incremental batch processing, and built-in data validation - rather than a basic multi-table JOIN. Maintenance guidance treats performance as an ongoing concern, not a one-time optimization pass: queries should be tested against realistic data volumes, complex business logic documented inline, and performance benchmarks maintained for critical reports so regressions are caught early rather than discovered in production as data volumes grow over time.

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.