mysql-advanced-sql/SKILL.md
Advanced MySQL 8 SQL techniques: window functions, CTEs, recursive queries, pivoting, JSON operations, stored procedures, triggers, and complex aggregations. Use when writing analytical queries, transforming data, implementing reporting SQL...
npx skillsauth add peterbamuhigire/skills-web-dev mysql-advanced-sqlInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
mysql-advanced-sql or would be better handled by a more specific companion skill.SKILL.md first, then load only the referenced deep-dive files that are necessary for the task.Expert patterns for MySQL 8+ beyond basic CRUD. Drawn from Leveling Up with SQL (Mark Simon, Apress 2023) plus production-hardened techniques.
Sections: Window Functions | Recursive CTEs | Pivoting | JSON | Gaps & Islands | Deduplication | Triggers | Stored Procedures | Conditional Aggregation | GROUP BY | Subqueries | Views | Anti-Patterns
Window functions compute a value per row over a related set of rows without collapsing them into a GROUP BY summary. Introduced in MySQL 8.0.
Syntax skeleton:
fn() OVER (
[PARTITION BY col, ...] -- subgroup (like GROUP BY)
[ORDER BY col [ASC|DESC]] -- enables cumulative / running calcs
[ROWS|RANGE frame_clause] -- optional sliding window
)
SELECT * FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC) AS rn
FROM orders
) t WHERE rn = 1;
| Function | Ties | Next rank after tie |
|---|---|---|
| ROW_NUMBER() | Arbitrary order | n+1 always |
| RANK() | Same rank | Skips (1,1,3) |
| DENSE_RANK() | Same rank | No skip (1,1,2) |
SELECT date, amount,
SUM(amount) OVER (
PARTITION BY customer_id
ORDER BY date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_total,
AVG(amount) OVER (
ORDER BY date
ROWS 6 PRECEDING -- 7-day rolling average
) AS week_avg
FROM orders;
ROWS vs RANGE:
ROWScounts physical rows;RANGEgroups rows with identical ORDER BY values. UseROWSfor running totals to avoid double-counting ties.
SELECT month, revenue,
LAG(revenue, 1) OVER (ORDER BY month) AS prev_month,
revenue - LAG(revenue, 1) OVER (ORDER BY month) AS change,
LEAD(revenue, 1) OVER (ORDER BY month) AS next_month
FROM monthly_revenue;
SELECT customer_id, total_spend,
NTILE(4) OVER (ORDER BY total_spend) AS quartile
FROM customer_totals;
Caution:
NTILEsplits by row count, so tied values can land in different tiles. For fair grouping useFLOOR((RANK() OVER (ORDER BY val) - 1) / bin_size) + 1.
SELECT *,
ROW_NUMBER() OVER w AS rn,
SUM(amount) OVER w AS running_total,
AVG(amount) OVER w AS running_avg
FROM orders
WINDOW w AS (PARTITION BY customer_id ORDER BY order_date
ROWS UNBOUNDED PRECEDING);
Extended guidance for mysql-advanced-sql was moved to references/skill-deep-dive.md to keep this entrypoint compact and fast to load.
Use that deep dive for:
2. Recursive CTEs3. Pivoting Data4. JSON Operations5. Gaps and Islands6. Deduplication7. Triggers8. Stored Procedures and Error Handling9. Conditional Aggregation10. Advanced GROUP BY11. Subqueries and CTEs as Variables12. Views and Sargable Date Patterns13. Anti-Patterns Referencedata-ai
Use when adding AI-powered analytics to a SaaS platform — semantic search over business data, natural language queries, trend detection, anomaly alerts, and AI-generated insights for dashboards. Covers embeddings, NL2SQL, and per-tenant analytics...
data-ai
Design AI-powered analytics dashboards — what metrics to show, how to display AI predictions and confidence, drill-down patterns, KPI cards, trend visualisation, AI Insights panels, export design, and role-based dashboard variants. Invoke when...
development
Use when designing, building, reviewing, or upgrading production software systems that must be secure, performant, maintainable, scalable, and user-centered. Apply before writing specs, code, architecture, APIs, databases, mobile apps, SaaS platforms, or ERP systems.
development
Professional web app UI using commercial templates (Tabler/Bootstrap 5) with strong frontend design direction when needed. Use for CRUD interfaces, dashboards, admin panels with SweetAlert2, DataTables, Flatpickr. Clone seeder-page.php, use...