Loading...
Loading...
Use when user needs SQL development, database design, query optimization, performance tuning, or database administration across PostgreSQL, MySQL, SQL Server, and Oracle platforms.
npx skill4agent add 404kidwiz/claude-supercode-skills sql-proQuery Requirement Analysis
│
├─ Need to reference result multiple times?
│ └─ YES → Use CTE (avoids duplicate subquery evaluation)
│ WITH user_totals AS (SELECT ...)
│ SELECT * FROM user_totals WHERE ...
│ UNION ALL
│ SELECT * FROM user_totals WHERE ...
│
├─ Recursive data traversal (hierarchy, graph)?
│ └─ YES → Use Recursive CTE (ONLY option for recursion)
│ WITH RECURSIVE tree AS (
│ SELECT ... -- anchor
│ UNION ALL
│ SELECT ... FROM tree ... -- recursive
│ )
│
├─ Simple lookup or filter?
│ └─ Use JOIN (most optimizable by query planner)
│ SELECT u.*, o.total
│ FROM users u
│ JOIN orders o ON u.id = o.user_id
│
├─ Correlated subquery in WHERE clause?
│ ├─ Checking existence → Use EXISTS (stops at first match)
│ │ WHERE EXISTS (SELECT 1 FROM orders WHERE user_id = u.id)
│ │
│ └─ Value comparison → Use JOIN instead
│ -- BAD: WHERE (SELECT COUNT(*) FROM orders WHERE user_id = users.id) > 5
│ -- GOOD: JOIN (SELECT user_id, COUNT(*) as cnt FROM orders GROUP BY user_id)
│
└─ Readability vs Performance trade-off?
├─ Complex logic, readability critical → CTE
│ (Easier to understand, debug, maintain)
│
└─ Performance critical, simple logic → Subquery or JOIN
(Query planner can inline and optimize)| Requirement | Solution | Example |
|---|---|---|
| Need aggregation + row-level detail | Window function | |
| Only aggregated results needed | GROUP BY | |
| Ranking/row numbering | Window function (ROW_NUMBER, RANK, DENSE_RANK) | |
| Running totals / moving averages | Window function with frame | |
| LAG/LEAD (access previous/next rows) | Window function | |
| Percentile / NTILE | Window function | |
| Simple count/sum/avg by group | GROUP BY (more efficient) | |
| Observation | Why Escalate | Example |
|---|---|---|
| Cartesian product in execution plan | Unintended cross join causing exponential rows | "Query returning millions of rows" |
| Complex multi-level recursive CTE performance | Advanced optimization needed | "Recursive CTE traversing 10+ levels with 100K nodes" |
| Cross-platform migration with incompatible features | Platform-specific feature mapping | "Migrating Oracle CONNECT BY to PostgreSQL recursive CTE" |
| Query with 10+ joins and complex logic | Architecture smell, potential redesign | "Single query joining 15 tables" |
| Temporal query with complex time-series logic | Advanced analytical pattern | "SCD Type 2 with historical snapshots" |