tribunal-kit 3.0.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (233) hide show
  1. package/.agent/ARCHITECTURE.md +99 -99
  2. package/.agent/GEMINI.md +52 -52
  3. package/.agent/agents/accessibility-reviewer.md +187 -220
  4. package/.agent/agents/ai-code-reviewer.md +199 -233
  5. package/.agent/agents/backend-specialist.md +215 -238
  6. package/.agent/agents/code-archaeologist.md +161 -181
  7. package/.agent/agents/database-architect.md +184 -207
  8. package/.agent/agents/debugger.md +191 -218
  9. package/.agent/agents/dependency-reviewer.md +103 -136
  10. package/.agent/agents/devops-engineer.md +218 -238
  11. package/.agent/agents/documentation-writer.md +201 -221
  12. package/.agent/agents/explorer-agent.md +160 -180
  13. package/.agent/agents/frontend-reviewer.md +160 -194
  14. package/.agent/agents/frontend-specialist.md +248 -237
  15. package/.agent/agents/game-developer.md +48 -52
  16. package/.agent/agents/logic-reviewer.md +116 -149
  17. package/.agent/agents/mobile-developer.md +200 -223
  18. package/.agent/agents/mobile-reviewer.md +162 -195
  19. package/.agent/agents/orchestrator.md +181 -211
  20. package/.agent/agents/penetration-tester.md +157 -174
  21. package/.agent/agents/performance-optimizer.md +183 -203
  22. package/.agent/agents/performance-reviewer.md +178 -211
  23. package/.agent/agents/precedence-reviewer.md +213 -0
  24. package/.agent/agents/product-manager.md +142 -162
  25. package/.agent/agents/product-owner.md +6 -25
  26. package/.agent/agents/project-planner.md +142 -162
  27. package/.agent/agents/qa-automation-engineer.md +225 -242
  28. package/.agent/agents/security-auditor.md +174 -194
  29. package/.agent/agents/seo-specialist.md +193 -213
  30. package/.agent/agents/sql-reviewer.md +161 -194
  31. package/.agent/agents/supervisor-agent.md +184 -203
  32. package/.agent/agents/swarm-worker-contracts.md +17 -17
  33. package/.agent/agents/swarm-worker-registry.md +46 -46
  34. package/.agent/agents/test-coverage-reviewer.md +160 -193
  35. package/.agent/agents/test-engineer.md +0 -21
  36. package/.agent/agents/type-safety-reviewer.md +175 -208
  37. package/.agent/patterns/generator.md +9 -9
  38. package/.agent/patterns/inversion.md +12 -12
  39. package/.agent/patterns/pipeline.md +9 -9
  40. package/.agent/patterns/reviewer.md +13 -13
  41. package/.agent/patterns/tool-wrapper.md +9 -9
  42. package/.agent/rules/GEMINI.md +63 -63
  43. package/.agent/scripts/append_flow.js +72 -0
  44. package/.agent/scripts/case_law_manager.py +525 -0
  45. package/.agent/scripts/compress_skills.py +167 -0
  46. package/.agent/scripts/consolidate_skills.py +173 -0
  47. package/.agent/scripts/deep_compress.py +202 -0
  48. package/.agent/scripts/minify_context.py +80 -0
  49. package/.agent/scripts/security_scan.py +1 -1
  50. package/.agent/scripts/skill_evolution.py +563 -0
  51. package/.agent/scripts/strip_tribunal.py +41 -0
  52. package/.agent/skills/agent-organizer/SKILL.md +100 -126
  53. package/.agent/skills/agentic-patterns/SKILL.md +0 -70
  54. package/.agent/skills/ai-prompt-injection-defense/SKILL.md +134 -160
  55. package/.agent/skills/api-patterns/SKILL.md +123 -215
  56. package/.agent/skills/api-security-auditor/SKILL.md +143 -177
  57. package/.agent/skills/app-builder/SKILL.md +334 -50
  58. package/.agent/skills/app-builder/templates/SKILL.md +13 -15
  59. package/.agent/skills/app-builder/templates/astro-static/TEMPLATE.md +16 -16
  60. package/.agent/skills/app-builder/templates/chrome-extension/TEMPLATE.md +22 -22
  61. package/.agent/skills/app-builder/templates/cli-tool/TEMPLATE.md +18 -18
  62. package/.agent/skills/app-builder/templates/electron-desktop/TEMPLATE.md +20 -20
  63. package/.agent/skills/app-builder/templates/express-api/TEMPLATE.md +17 -17
  64. package/.agent/skills/app-builder/templates/flutter-app/TEMPLATE.md +18 -18
  65. package/.agent/skills/app-builder/templates/monorepo-turborepo/TEMPLATE.md +21 -21
  66. package/.agent/skills/app-builder/templates/nextjs-fullstack/TEMPLATE.md +19 -19
  67. package/.agent/skills/app-builder/templates/nextjs-saas/TEMPLATE.md +26 -26
  68. package/.agent/skills/app-builder/templates/nextjs-static/TEMPLATE.md +26 -26
  69. package/.agent/skills/app-builder/templates/nuxt-app/TEMPLATE.md +19 -19
  70. package/.agent/skills/app-builder/templates/python-fastapi/TEMPLATE.md +18 -18
  71. package/.agent/skills/app-builder/templates/react-native-app/TEMPLATE.md +20 -20
  72. package/.agent/skills/appflow-wireframe/SKILL.md +95 -121
  73. package/.agent/skills/architecture/SKILL.md +169 -331
  74. package/.agent/skills/authentication-best-practices/SKILL.md +139 -173
  75. package/.agent/skills/bash-linux/SKILL.md +129 -154
  76. package/.agent/skills/behavioral-modes/SKILL.md +8 -69
  77. package/.agent/skills/brainstorming/SKILL.md +436 -104
  78. package/.agent/skills/building-native-ui/SKILL.md +152 -174
  79. package/.agent/skills/clean-code/SKILL.md +331 -360
  80. package/.agent/skills/code-review-checklist/SKILL.md +0 -62
  81. package/.agent/skills/config-validator/SKILL.md +115 -141
  82. package/.agent/skills/csharp-developer/SKILL.md +468 -528
  83. package/.agent/skills/database-design/SKILL.md +104 -369
  84. package/.agent/skills/deployment-procedures/SKILL.md +119 -145
  85. package/.agent/skills/devops-engineer/SKILL.md +295 -332
  86. package/.agent/skills/devops-incident-responder/SKILL.md +87 -113
  87. package/.agent/skills/doc.md +5 -5
  88. package/.agent/skills/documentation-templates/SKILL.md +27 -63
  89. package/.agent/skills/edge-computing/SKILL.md +131 -157
  90. package/.agent/skills/extract-design-system/SKILL.md +108 -134
  91. package/.agent/skills/framer-motion-expert/SKILL.md +111 -855
  92. package/.agent/skills/frontend-design/SKILL.md +151 -499
  93. package/.agent/skills/game-design-expert/SKILL.md +79 -105
  94. package/.agent/skills/game-engineering-expert/SKILL.md +96 -122
  95. package/.agent/skills/geo-fundamentals/SKILL.md +97 -124
  96. package/.agent/skills/github-operations/SKILL.md +279 -314
  97. package/.agent/skills/gsap-expert/SKILL.md +119 -826
  98. package/.agent/skills/i18n-localization/SKILL.md +113 -138
  99. package/.agent/skills/intelligent-routing/SKILL.md +167 -127
  100. package/.agent/skills/lint-and-validate/SKILL.md +16 -52
  101. package/.agent/skills/llm-engineering/SKILL.md +344 -357
  102. package/.agent/skills/local-first/SKILL.md +128 -154
  103. package/.agent/skills/mcp-builder/SKILL.md +92 -118
  104. package/.agent/skills/mobile-design/SKILL.md +213 -219
  105. package/.agent/skills/motion-engineering/SKILL.md +184 -0
  106. package/.agent/skills/nextjs-react-expert/SKILL.md +99 -698
  107. package/.agent/skills/nodejs-best-practices/SKILL.md +498 -559
  108. package/.agent/skills/observability/SKILL.md +293 -330
  109. package/.agent/skills/parallel-agents/SKILL.md +96 -122
  110. package/.agent/skills/performance-profiling/SKILL.md +217 -254
  111. package/.agent/skills/plan-writing/SKILL.md +92 -118
  112. package/.agent/skills/platform-engineer/SKILL.md +97 -123
  113. package/.agent/skills/playwright-best-practices/SKILL.md +137 -162
  114. package/.agent/skills/powershell-windows/SKILL.md +112 -146
  115. package/.agent/skills/project-idioms/SKILL.md +87 -0
  116. package/.agent/skills/python-patterns/SKILL.md +15 -35
  117. package/.agent/skills/python-pro/SKILL.md +148 -754
  118. package/.agent/skills/react-specialist/SKILL.md +123 -827
  119. package/.agent/skills/readme-builder/SKILL.md +23 -85
  120. package/.agent/skills/realtime-patterns/SKILL.md +269 -304
  121. package/.agent/skills/red-team-tactics/SKILL.md +18 -51
  122. package/.agent/skills/rust-pro/SKILL.md +623 -701
  123. package/.agent/skills/seo-fundamentals/SKILL.md +129 -154
  124. package/.agent/skills/server-management/SKILL.md +164 -190
  125. package/.agent/skills/shadcn-ui-expert/SKILL.md +181 -206
  126. package/.agent/skills/skill-creator/SKILL.md +24 -56
  127. package/.agent/skills/sql-pro/SKILL.md +579 -633
  128. package/.agent/skills/supabase-postgres-best-practices/SKILL.md +35 -66
  129. package/.agent/skills/swiftui-expert/SKILL.md +151 -176
  130. package/.agent/skills/systematic-debugging/SKILL.md +92 -118
  131. package/.agent/skills/tailwind-patterns/SKILL.md +516 -576
  132. package/.agent/skills/tdd-workflow/SKILL.md +111 -137
  133. package/.agent/skills/test-result-analyzer/SKILL.md +33 -73
  134. package/.agent/skills/testing-patterns/SKILL.md +512 -573
  135. package/.agent/skills/trend-researcher/SKILL.md +30 -71
  136. package/.agent/skills/ui-ux-pro-max/SKILL.md +8 -41
  137. package/.agent/skills/ui-ux-researcher/SKILL.md +51 -91
  138. package/.agent/skills/vue-expert/SKILL.md +127 -866
  139. package/.agent/skills/vulnerability-scanner/SKILL.md +354 -269
  140. package/.agent/skills/web-accessibility-auditor/SKILL.md +168 -193
  141. package/.agent/skills/web-design-guidelines/SKILL.md +25 -61
  142. package/.agent/skills/webapp-testing/SKILL.md +119 -145
  143. package/.agent/skills/whimsy-injector/SKILL.md +58 -132
  144. package/.agent/skills/workflow-optimizer/SKILL.md +28 -68
  145. package/.agent/workflows/api-tester.md +151 -151
  146. package/.agent/workflows/audit.md +127 -138
  147. package/.agent/workflows/brainstorm.md +110 -110
  148. package/.agent/workflows/changelog.md +112 -112
  149. package/.agent/workflows/create.md +124 -124
  150. package/.agent/workflows/debug.md +165 -189
  151. package/.agent/workflows/deploy.md +180 -189
  152. package/.agent/workflows/enhance.md +128 -151
  153. package/.agent/workflows/fix.md +114 -135
  154. package/.agent/workflows/generate.md +13 -4
  155. package/.agent/workflows/migrate.md +160 -160
  156. package/.agent/workflows/orchestrate.md +168 -168
  157. package/.agent/workflows/performance-benchmarker.md +114 -123
  158. package/.agent/workflows/plan.md +173 -173
  159. package/.agent/workflows/preview.md +80 -80
  160. package/.agent/workflows/refactor.md +161 -183
  161. package/.agent/workflows/review-ai.md +101 -129
  162. package/.agent/workflows/review.md +116 -116
  163. package/.agent/workflows/session.md +94 -94
  164. package/.agent/workflows/status.md +79 -79
  165. package/.agent/workflows/strengthen-skills.md +138 -139
  166. package/.agent/workflows/swarm.md +179 -179
  167. package/.agent/workflows/test.md +189 -211
  168. package/.agent/workflows/tribunal-backend.md +94 -113
  169. package/.agent/workflows/tribunal-database.md +95 -115
  170. package/.agent/workflows/tribunal-frontend.md +96 -118
  171. package/.agent/workflows/tribunal-full.md +93 -133
  172. package/.agent/workflows/tribunal-mobile.md +95 -119
  173. package/.agent/workflows/tribunal-performance.md +110 -133
  174. package/.agent/workflows/ui-ux-pro-max.md +122 -143
  175. package/README.md +30 -1
  176. package/bin/tribunal-kit.js +175 -12
  177. package/package.json +25 -4
  178. package/.agent/skills/api-patterns/api-style.md +0 -42
  179. package/.agent/skills/api-patterns/auth.md +0 -24
  180. package/.agent/skills/api-patterns/documentation.md +0 -26
  181. package/.agent/skills/api-patterns/graphql.md +0 -41
  182. package/.agent/skills/api-patterns/rate-limiting.md +0 -31
  183. package/.agent/skills/api-patterns/response.md +0 -37
  184. package/.agent/skills/api-patterns/rest.md +0 -40
  185. package/.agent/skills/api-patterns/security-testing.md +0 -122
  186. package/.agent/skills/api-patterns/trpc.md +0 -41
  187. package/.agent/skills/api-patterns/versioning.md +0 -22
  188. package/.agent/skills/app-builder/agent-coordination.md +0 -71
  189. package/.agent/skills/app-builder/feature-building.md +0 -53
  190. package/.agent/skills/app-builder/project-detection.md +0 -34
  191. package/.agent/skills/app-builder/scaffolding.md +0 -118
  192. package/.agent/skills/app-builder/tech-stack.md +0 -40
  193. package/.agent/skills/architecture/context-discovery.md +0 -43
  194. package/.agent/skills/architecture/examples.md +0 -94
  195. package/.agent/skills/architecture/pattern-selection.md +0 -68
  196. package/.agent/skills/architecture/patterns-reference.md +0 -50
  197. package/.agent/skills/architecture/trade-off-analysis.md +0 -77
  198. package/.agent/skills/brainstorming/dynamic-questioning.md +0 -360
  199. package/.agent/skills/database-design/database-selection.md +0 -43
  200. package/.agent/skills/database-design/indexing.md +0 -39
  201. package/.agent/skills/database-design/migrations.md +0 -48
  202. package/.agent/skills/database-design/optimization.md +0 -36
  203. package/.agent/skills/database-design/orm-selection.md +0 -30
  204. package/.agent/skills/database-design/schema-design.md +0 -56
  205. package/.agent/skills/frontend-design/animation-guide.md +0 -331
  206. package/.agent/skills/frontend-design/color-system.md +0 -329
  207. package/.agent/skills/frontend-design/decision-trees.md +0 -418
  208. package/.agent/skills/frontend-design/motion-graphics.md +0 -306
  209. package/.agent/skills/frontend-design/typography-system.md +0 -363
  210. package/.agent/skills/frontend-design/ux-psychology.md +0 -1116
  211. package/.agent/skills/frontend-design/visual-effects.md +0 -383
  212. package/.agent/skills/intelligent-routing/router-manifest.md +0 -65
  213. package/.agent/skills/mobile-design/decision-trees.md +0 -516
  214. package/.agent/skills/mobile-design/mobile-backend.md +0 -491
  215. package/.agent/skills/mobile-design/mobile-color-system.md +0 -420
  216. package/.agent/skills/mobile-design/mobile-debugging.md +0 -122
  217. package/.agent/skills/mobile-design/mobile-design-thinking.md +0 -357
  218. package/.agent/skills/mobile-design/mobile-navigation.md +0 -458
  219. package/.agent/skills/mobile-design/mobile-performance.md +0 -767
  220. package/.agent/skills/mobile-design/mobile-testing.md +0 -356
  221. package/.agent/skills/mobile-design/mobile-typography.md +0 -433
  222. package/.agent/skills/mobile-design/platform-android.md +0 -666
  223. package/.agent/skills/mobile-design/platform-ios.md +0 -561
  224. package/.agent/skills/mobile-design/touch-psychology.md +0 -537
  225. package/.agent/skills/nextjs-react-expert/1-async-eliminating-waterfalls.md +0 -312
  226. package/.agent/skills/nextjs-react-expert/2-bundle-bundle-size-optimization.md +0 -240
  227. package/.agent/skills/nextjs-react-expert/3-server-server-side-performance.md +0 -490
  228. package/.agent/skills/nextjs-react-expert/4-client-client-side-data-fetching.md +0 -264
  229. package/.agent/skills/nextjs-react-expert/5-rerender-re-render-optimization.md +0 -581
  230. package/.agent/skills/nextjs-react-expert/6-rendering-rendering-performance.md +0 -432
  231. package/.agent/skills/nextjs-react-expert/7-js-javascript-performance.md +0 -684
  232. package/.agent/skills/nextjs-react-expert/8-advanced-advanced-patterns.md +0 -150
  233. package/.agent/skills/vulnerability-scanner/checklists.md +0 -121
@@ -1,633 +1,579 @@
1
- ---
2
- name: sql-pro
3
- description: Senior SQL developer across major databases (PostgreSQL, MySQL, SQL Server, Oracle). Complex query design with CTEs, window functions, PIVOT, recursive queries, JSON operations, full-text search, performance optimization with EXPLAIN ANALYZE, indexing strategies, partitioning, and schema architecture. Use when writing queries, designing schemas, optimizing performance, or debugging slow queries.
4
- allowed-tools: Read, Write, Edit, Glob, Grep
5
- version: 2.0.0
6
- last-updated: 2026-03-30
7
- applies-to-model: gemini-2.5-pro, claude-3-7-sonnet
8
- ---
9
-
10
- # SQL Pro — Advanced Query & Schema Mastery
11
-
12
- > SQL is not "just queries." It is a declarative, set-based computation engine.
13
- > Every query must be SARGable. Every join must be indexed. Every migration must be reversible. No exceptions.
14
-
15
- ---
16
-
17
- ## Common Table Expressions (CTEs)
18
-
19
- ### Basic CTE
20
-
21
- ```sql
22
- -- CTE for readability and reuse
23
- WITH active_users AS (
24
- SELECT id, name, email, created_at
25
- FROM users
26
- WHERE is_active = true
27
- AND last_login > CURRENT_DATE - INTERVAL '30 days'
28
- ),
29
- user_orders AS (
30
- SELECT user_id, COUNT(*) AS order_count, SUM(total) AS total_spent
31
- FROM orders
32
- WHERE status = 'completed'
33
- GROUP BY user_id
34
- )
35
- SELECT
36
- u.name,
37
- u.email,
38
- COALESCE(o.order_count, 0) AS orders,
39
- COALESCE(o.total_spent, 0) AS revenue
40
- FROM active_users u
41
- LEFT JOIN user_orders o ON u.id = o.user_id
42
- ORDER BY revenue DESC;
43
- ```
44
-
45
- ### Recursive CTE (Hierarchical Data)
46
-
47
- ```sql
48
- -- Org chart: find all reports under a manager
49
- WITH RECURSIVE org_tree AS (
50
- -- Base case: the starting manager
51
- SELECT id, name, manager_id, 1 AS depth
52
- FROM employees
53
- WHERE id = 42 -- starting point
54
-
55
- UNION ALL
56
-
57
- -- Recursive case: find direct reports
58
- SELECT e.id, e.name, e.manager_id, t.depth + 1
59
- FROM employees e
60
- INNER JOIN org_tree t ON e.manager_id = t.id
61
- WHERE t.depth < 10 -- safety limit to prevent infinite loops
62
- )
63
- SELECT * FROM org_tree ORDER BY depth, name;
64
-
65
- -- HALLUCINATION TRAP: Always include a depth/cycle guard
66
- -- Without it, circular references cause infinite recursion
67
- -- PostgreSQL: use CYCLE detection clause (PG 14+)
68
- -- SQL Server: use MAXRECURSION option
69
- ```
70
-
71
- ### CTE for Running Totals & Pagination
72
-
73
- ```sql
74
- -- Keyset pagination (faster than OFFSET for large tables)
75
- WITH page AS (
76
- SELECT id, name, created_at
77
- FROM products
78
- WHERE (created_at, id) < (:last_created_at, :last_id) -- cursor
79
- ORDER BY created_at DESC, id DESC
80
- LIMIT 20
81
- )
82
- SELECT * FROM page;
83
-
84
- -- ❌ HALLUCINATION TRAP: OFFSET-based pagination gets slower with higher pages
85
- -- OFFSET 100000, LIMIT 20 scans and discards 100,000 rows
86
- -- Keyset pagination is O(1) regardless of page number
87
- ```
88
-
89
- ---
90
-
91
- ## Window Functions
92
-
93
- ### Ranking Functions
94
-
95
- ```sql
96
- -- ROW_NUMBER: unique sequential number per partition
97
- SELECT
98
- department,
99
- name,
100
- salary,
101
- ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rank_in_dept
102
- FROM employees;
103
-
104
- -- RANK vs DENSE_RANK
105
- -- RANK: 1, 2, 2, 4 (gaps after ties)
106
- -- DENSE_RANK: 1, 2, 2, 3 (no gaps)
107
- SELECT
108
- name,
109
- score,
110
- RANK() OVER (ORDER BY score DESC) AS rank,
111
- DENSE_RANK() OVER (ORDER BY score DESC) AS dense_rank
112
- FROM leaderboard;
113
-
114
- -- NTILE: divide into N equal groups
115
- SELECT
116
- name,
117
- revenue,
118
- NTILE(4) OVER (ORDER BY revenue DESC) AS quartile
119
- FROM companies;
120
- ```
121
-
122
- ### Aggregate Windows
123
-
124
- ```sql
125
- -- Running total
126
- SELECT
127
- date,
128
- amount,
129
- SUM(amount) OVER (ORDER BY date) AS running_total,
130
- AVG(amount) OVER (ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS moving_avg_7d
131
- FROM daily_revenue;
132
-
133
- -- Percentage of total
134
- SELECT
135
- category,
136
- revenue,
137
- ROUND(100.0 * revenue / SUM(revenue) OVER (), 2) AS pct_of_total
138
- FROM category_sales;
139
-
140
- -- Difference from previous row
141
- SELECT
142
- month,
143
- revenue,
144
- revenue - LAG(revenue) OVER (ORDER BY month) AS mom_change,
145
- ROUND(100.0 * (revenue - LAG(revenue) OVER (ORDER BY month))
146
- / NULLIF(LAG(revenue) OVER (ORDER BY month), 0), 2) AS mom_pct_change
147
- FROM monthly_revenue;
148
- ```
149
-
150
- ### Frame Clauses
151
-
152
- ```sql
153
- -- Frame clause controls which rows the window function sees
154
- SUM(amount) OVER (
155
- ORDER BY date
156
- ROWS BETWEEN 2 PRECEDING AND CURRENT ROW -- last 3 rows (physical)
157
- )
158
-
159
- SUM(amount) OVER (
160
- ORDER BY date
161
- RANGE BETWEEN INTERVAL '7 days' PRECEDING AND CURRENT ROW -- last 7 days (logical)
162
- )
163
-
164
- -- ROWS vs RANGE:
165
- -- ROWS = physical row count (exact)
166
- -- RANGE = logical value range (handles ties differently)
167
-
168
- -- ❌ HALLUCINATION TRAP: Default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
169
- -- This means SUM() OVER (ORDER BY x) includes ALL preceding rows, not just "the one before"
170
- -- To get a true running count of N rows, use ROWS BETWEEN explicitly
171
- ```
172
-
173
- ### Lead / Lag Analysis
174
-
175
- ```sql
176
- -- Next and previous values
177
- SELECT
178
- event_date,
179
- user_id,
180
- LAG(event_date) OVER (PARTITION BY user_id ORDER BY event_date) AS prev_visit,
181
- LEAD(event_date) OVER (PARTITION BY user_id ORDER BY event_date) AS next_visit,
182
- event_date - LAG(event_date) OVER (PARTITION BY user_id ORDER BY event_date) AS days_between
183
- FROM user_events;
184
-
185
- -- FIRST_VALUE / LAST_VALUE
186
- SELECT
187
- department,
188
- name,
189
- salary,
190
- FIRST_VALUE(name) OVER (PARTITION BY department ORDER BY salary DESC) AS highest_paid,
191
- LAST_VALUE(name) OVER (
192
- PARTITION BY department ORDER BY salary DESC
193
- ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING -- ⚠️ required!
194
- ) AS lowest_paid
195
- FROM employees;
196
-
197
- -- ❌ HALLUCINATION TRAP: LAST_VALUE without explicit frame clause returns CURRENT ROW
198
- -- You MUST specify ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
199
- ```
200
-
201
- ---
202
-
203
- ## PIVOT / UNPIVOT / Conditional Aggregation
204
-
205
- ### PostgreSQL / Standard SQL (Conditional Aggregation)
206
-
207
- ```sql
208
- -- PostgreSQL doesn't have PIVOT use conditional aggregation
209
- SELECT
210
- product_name,
211
- SUM(CASE WHEN quarter = 'Q1' THEN revenue ELSE 0 END) AS q1,
212
- SUM(CASE WHEN quarter = 'Q2' THEN revenue ELSE 0 END) AS q2,
213
- SUM(CASE WHEN quarter = 'Q3' THEN revenue ELSE 0 END) AS q3,
214
- SUM(CASE WHEN quarter = 'Q4' THEN revenue ELSE 0 END) AS q4,
215
- SUM(revenue) AS total
216
- FROM quarterly_sales
217
- GROUP BY product_name
218
- ORDER BY total DESC;
219
-
220
- -- PostgreSQL crosstab (requires tablefunc extension)
221
- SELECT * FROM crosstab(
222
- 'SELECT product, quarter, revenue FROM sales ORDER BY 1, 2',
223
- 'SELECT DISTINCT quarter FROM sales ORDER BY 1'
224
- ) AS ct(product TEXT, q1 NUMERIC, q2 NUMERIC, q3 NUMERIC, q4 NUMERIC);
225
- ```
226
-
227
- ### SQL Server PIVOT
228
-
229
- ```sql
230
- -- SQL Server native PIVOT
231
- SELECT *
232
- FROM (
233
- SELECT product_name, quarter, revenue
234
- FROM quarterly_sales
235
- ) AS source
236
- PIVOT (
237
- SUM(revenue)
238
- FOR quarter IN ([Q1], [Q2], [Q3], [Q4])
239
- ) AS pivoted;
240
- ```
241
-
242
- ---
243
-
244
- ## JSON Operations
245
-
246
- ### PostgreSQL JSONB
247
-
248
- ```sql
249
- -- Query JSON fields
250
- SELECT
251
- id,
252
- profile->>'name' AS name, -- text extraction
253
- profile->'address'->>'city' AS city, -- nested extraction
254
- (profile->>'age')::int AS age -- cast to int
255
- FROM users
256
- WHERE profile->>'country' = 'US'
257
- AND (profile->>'age')::int >= 18;
258
-
259
- -- JSONB containment
260
- SELECT * FROM products
261
- WHERE metadata @> '{"category": "electronics"}'; -- contains
262
-
263
- -- JSONB existence
264
- SELECT * FROM products
265
- WHERE metadata ? 'warranty'; -- key exists
266
-
267
- -- JSONB array queries
268
- SELECT * FROM users
269
- WHERE profile->'tags' ? 'premium'; -- array contains value
270
-
271
- -- Update JSONB
272
- UPDATE users
273
- SET profile = jsonb_set(profile, '{address,city}', '"New York"')
274
- WHERE id = 1;
275
-
276
- -- JSONB aggregation
277
- SELECT jsonb_agg(jsonb_build_object('id', id, 'name', name)) AS users_json
278
- FROM users
279
- WHERE is_active = true;
280
-
281
- -- HALLUCINATION TRAP: -> returns JSON, ->> returns TEXT
282
- -- Filtering on -> requires JSON comparison
283
- -- Filtering on ->> allows string comparison
284
- -- Always cast ->> results when comparing numbers: (col->>'age')::int
285
- ```
286
-
287
- ---
288
-
289
- ## Indexing Strategy
290
-
291
- ### Index Types
292
-
293
- ```sql
294
- -- B-tree (default good for equality and range)
295
- CREATE INDEX idx_users_email ON users (email);
296
-
297
- -- Composite index (column order matters!)
298
- CREATE INDEX idx_orders_user_date ON orders (user_id, created_at DESC);
299
- -- ✅ Supports: WHERE user_id = 1 AND created_at > '2024-01-01'
300
- -- Supports: WHERE user_id = 1 (uses leftmost prefix)
301
- -- Does NOT support: WHERE created_at > '2024-01-01' (skips first column)
302
-
303
- -- Partial / Filtered index (index only matching rows)
304
- CREATE INDEX idx_active_users ON users (email) WHERE is_active = true;
305
- -- Smaller index, faster queries when filtering by is_active
306
-
307
- -- Covering index (INCLUDE avoids table lookup)
308
- CREATE INDEX idx_orders_covering ON orders (user_id)
309
- INCLUDE (total, status, created_at);
310
- -- All needed columns in the index = index-only scan
311
-
312
- -- GIN index (for JSONB, arrays, full-text search)
313
- CREATE INDEX idx_products_metadata ON products USING gin (metadata);
314
-
315
- -- GiST index (for geometric, range, full-text)
316
- CREATE INDEX idx_locations_geo ON locations USING gist (coordinates);
317
-
318
- -- BRIN index (for naturally ordered data like timestamps)
319
- CREATE INDEX idx_logs_timestamp ON logs USING brin (created_at);
320
- -- Tiny index, perfect for append-only tables with timestamp ordering
321
- ```
322
-
323
- ### SARGability
324
-
325
- ```sql
326
- -- SARGable = Search ARGument ABLE — can the query use an index?
327
-
328
- -- SARGable (index seekable)
329
- WHERE created_at >= '2024-01-01'
330
- WHERE email = 'alice@test.com'
331
- WHERE name LIKE 'Ali%' -- prefix match
332
-
333
- -- NOT SARGable (forces full table scan)
334
- WHERE YEAR(created_at) = 2024 -- function on column
335
- WHERE LOWER(email) = 'alice@test.com' -- function on column
336
- WHERE name LIKE '%alice%' -- leading wildcard
337
- WHERE amount + tax > 100 -- expression on column
338
- WHERE COALESCE(name, '') = '' -- function on column
339
-
340
- -- ✅ Fix: functional index (PostgreSQL)
341
- CREATE INDEX idx_users_email_lower ON users (LOWER(email));
342
- -- Now WHERE LOWER(email) = 'alice@test.com' IS SARGable
343
-
344
- -- ✅ Fix: computed column (SQL Server)
345
- ALTER TABLE users ADD email_lower AS LOWER(email) PERSISTED;
346
- CREATE INDEX idx_email_lower ON users (email_lower);
347
-
348
- -- ❌ HALLUCINATION TRAP: Implicit type conversions destroy SARGability
349
- -- WHERE varchar_column = 123 ← implicit cast on EVERY row
350
- -- WHERE varchar_column = '123' ← direct comparison, uses index
351
- ```
352
-
353
- ---
354
-
355
- ## EXPLAIN ANALYZE (Query Optimization)
356
-
357
- ```sql
358
- -- PostgreSQL
359
- EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
360
- SELECT * FROM orders WHERE user_id = 42 AND status = 'completed';
361
-
362
- -- Reading the output:
363
- -- Seq Scan full table scan (usually bad for large tables)
364
- -- Index Scan using an index to find rows (good)
365
- -- Index Only Scan all data from index, no table access (best)
366
- -- Bitmap Scan index + bitmap for multiple conditions (good for moderate selectivity)
367
- -- Hash Join → building hash table for join (good for large joins)
368
- -- Nested Loop → for each row in A, scan B (good for small datasets, bad for large)
369
- -- Sort explicit sorting (check if index can avoid this)
370
-
371
- -- Key metrics:
372
- -- actual time=X..Y → X = time to first row, Y = time to all rows (ms)
373
- -- rows=N actual rows returned
374
- -- loops=N → number of times this node executed
375
- -- Buffers: shared hit=N → pages read from cache (good)
376
- -- Buffers: shared read=N → pages read from disk (measure of I/O cost)
377
-
378
- -- SQL Server
379
- SET STATISTICS IO ON;
380
- SET STATISTICS TIME ON;
381
- SELECT * FROM orders WHERE user_id = 42;
382
- -- Check: logical reads (from cache), physical reads (from disk)
383
-
384
- -- HALLUCINATION TRAP: EXPLAIN without ANALYZE shows estimates, NOT actuals
385
- -- Always use EXPLAIN ANALYZE for real performance data
386
- -- But CAREFUL: ANALYZE actually EXECUTES the query
387
- -- For destructive queries (DELETE, UPDATE), wrap in a transaction:
388
- BEGIN;
389
- EXPLAIN ANALYZE DELETE FROM users WHERE id = 1;
390
- ROLLBACK; -- prevents actual deletion
391
- ```
392
-
393
- ---
394
-
395
- ## Table Partitioning
396
-
397
- ```sql
398
- -- PostgreSQL range partitioning (ideal for time-series data)
399
- CREATE TABLE events (
400
- id BIGSERIAL,
401
- event_type TEXT NOT NULL,
402
- payload JSONB,
403
- created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
404
- ) PARTITION BY RANGE (created_at);
405
-
406
- -- Create monthly partitions
407
- CREATE TABLE events_2024_01 PARTITION OF events
408
- FOR VALUES FROM ('2024-01-01') TO ('2024-02-01');
409
- CREATE TABLE events_2024_02 PARTITION OF events
410
- FOR VALUES FROM ('2024-02-01') TO ('2024-03-01');
411
-
412
- -- Queries automatically prune partitions:
413
- -- SELECT * FROM events WHERE created_at >= '2024-02-01'
414
- -- Only scans events_2024_02 and later — skips events_2024_01 entirely
415
-
416
- -- List partitioning (for categorical data)
417
- CREATE TABLE orders (
418
- id SERIAL,
419
- region TEXT NOT NULL,
420
- total NUMERIC
421
- ) PARTITION BY LIST (region);
422
-
423
- CREATE TABLE orders_us PARTITION OF orders FOR VALUES IN ('US');
424
- CREATE TABLE orders_eu PARTITION OF orders FOR VALUES IN ('EU', 'UK');
425
- CREATE TABLE orders_apac PARTITION OF orders FOR VALUES IN ('JP', 'KR', 'AU');
426
- ```
427
-
428
- ---
429
-
430
- ## Transactions & Concurrency
431
-
432
- ```sql
433
- -- Proper transaction pattern
434
- BEGIN;
435
-
436
- -- Lock the row for update (prevents concurrent modification)
437
- SELECT * FROM accounts WHERE id = 1 FOR UPDATE;
438
-
439
- UPDATE accounts SET balance = balance - 100 WHERE id = 1;
440
- UPDATE accounts SET balance = balance + 100 WHERE id = 2;
441
-
442
- -- Verify consistency
443
- DO $$
444
- BEGIN
445
- IF (SELECT SUM(balance) FROM accounts) <> 10000 THEN
446
- RAISE EXCEPTION 'Balance integrity violation';
447
- END IF;
448
- END $$;
449
-
450
- COMMIT;
451
-
452
- -- Isolation levels (ordered by strictness)
453
- SET TRANSACTION ISOLATION LEVEL READ COMMITTED; -- default (PostgreSQL)
454
- SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; -- snapshot isolation
455
- SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; -- strictest (may abort)
456
-
457
- -- Advisory locks (application-level locking)
458
- SELECT pg_advisory_lock(12345); -- acquire
459
- -- ... do work ...
460
- SELECT pg_advisory_unlock(12345); -- release
461
- ```
462
-
463
- ---
464
-
465
- ## MERGE / UPSERT
466
-
467
- ```sql
468
- -- PostgreSQL UPSERT (ON CONFLICT)
469
- INSERT INTO products (sku, name, price, updated_at)
470
- VALUES ('ABC-123', 'Widget', 29.99, NOW())
471
- ON CONFLICT (sku) DO UPDATE SET
472
- name = EXCLUDED.name,
473
- price = EXCLUDED.price,
474
- updated_at = EXCLUDED.updated_at;
475
-
476
- -- SQL Server MERGE
477
- MERGE INTO products AS target
478
- USING staging_products AS source
479
- ON target.sku = source.sku
480
- WHEN MATCHED THEN
481
- UPDATE SET
482
- name = source.name,
483
- price = source.price,
484
- updated_at = GETDATE()
485
- WHEN NOT MATCHED THEN
486
- INSERT (sku, name, price, created_at)
487
- VALUES (source.sku, source.name, source.price, GETDATE());
488
-
489
- -- ❌ HALLUCINATION TRAP: PostgreSQL uses ON CONFLICT, not MERGE
490
- -- MERGE was added in PostgreSQL 15+ but ON CONFLICT is idiomatic
491
- -- SQL Server uses MERGE — do not confuse the two syntaxes
492
- ```
493
-
494
- ---
495
-
496
- ## Full-Text Search (PostgreSQL)
497
-
498
- ```sql
499
- -- Create tsvector column and GIN index
500
- ALTER TABLE articles ADD COLUMN search_vector tsvector;
501
-
502
- UPDATE articles SET search_vector =
503
- setweight(to_tsvector('english', COALESCE(title, '')), 'A') ||
504
- setweight(to_tsvector('english', COALESCE(body, '')), 'B');
505
-
506
- CREATE INDEX idx_articles_search ON articles USING gin (search_vector);
507
-
508
- -- Search with ranking
509
- SELECT
510
- title,
511
- ts_rank(search_vector, query) AS rank
512
- FROM articles, to_tsquery('english', 'database & optimization') AS query
513
- WHERE search_vector @@ query
514
- ORDER BY rank DESC
515
- LIMIT 20;
516
-
517
- -- Trigger to auto-update search vector
518
- CREATE FUNCTION update_search_vector() RETURNS trigger AS $$
519
- BEGIN
520
- NEW.search_vector :=
521
- setweight(to_tsvector('english', COALESCE(NEW.title, '')), 'A') ||
522
- setweight(to_tsvector('english', COALESCE(NEW.body, '')), 'B');
523
- RETURN NEW;
524
- END $$ LANGUAGE plpgsql;
525
-
526
- CREATE TRIGGER trg_articles_search
527
- BEFORE INSERT OR UPDATE OF title, body ON articles
528
- FOR EACH ROW EXECUTE FUNCTION update_search_vector();
529
- ```
530
-
531
- ---
532
-
533
- ## Security
534
-
535
- ```sql
536
- -- ✅ ALWAYS use parameterized queries
537
- -- PostgreSQL (via psycopg)
538
- cursor.execute("SELECT * FROM users WHERE email = %s", (email,))
539
-
540
- -- Python SQLAlchemy
541
- stmt = select(User).where(User.email == email)
542
-
543
- -- NEVER: String interpolation for SQL
544
- -- ❌ f"SELECT * FROM users WHERE email = '{email}'"
545
- -- "SELECT * FROM users WHERE email = '" + email + "'"
546
- -- These allow SQL injection: email = "'; DROP TABLE users; --"
547
-
548
- -- Row-Level Security (PostgreSQL)
549
- ALTER TABLE documents ENABLE ROW LEVEL SECURITY;
550
-
551
- CREATE POLICY documents_owner_policy ON documents
552
- USING (owner_id = current_setting('app.current_user_id')::int);
553
-
554
- -- Grant minimum permissions
555
- GRANT SELECT, INSERT ON users TO app_role;
556
- -- ❌ NEVER: GRANT ALL ON DATABASE TO app_role
557
- ```
558
-
559
- ---
560
-
561
- ## Output Format
562
-
563
- When this skill produces or reviews code, structure your output as follows:
564
-
565
- ```
566
- ━━━ SQL Pro Report ━━━━━━━━━━━━━━━━━━━━━━━━
567
- Skill: SQL Pro
568
- Database: [PostgreSQL/MySQL/SQL Server/Oracle]
569
- Scope: [N queries · N tables]
570
- ─────────────────────────────────────────────────
571
- ✅ Passed: [checks that passed, or "All clean"]
572
- ⚠️ Warnings: [non-blocking issues, or "None"]
573
- ❌ Blocked: [blocking issues requiring fix, or "None"]
574
- ─────────────────────────────────────────────────
575
- VBC status: PENDING → VERIFIED
576
- Evidence: [EXPLAIN ANALYZE output / migration success / test pass]
577
- ```
578
-
579
- **VBC (Verification-Before-Completion) is mandatory.**
580
- Do not mark status as VERIFIED until concrete terminal evidence is provided.
581
-
582
- ---
583
-
584
- ## 🤖 LLM-Specific Traps
585
-
586
- AI coding assistants often fall into specific bad habits when generating SQL. These are strictly forbidden:
587
-
588
- 1. **`SELECT *` in Production:** Never use `SELECT *`. Always specify exact columns needed. It wastes bandwidth, breaks covering indexes, and hides schema changes.
589
- 2. **String-Interpolated SQL:** Never use f-strings, `.format()`, or string concatenation to build queries. Always use parameterized queries or ORM query builders.
590
- 3. **OFFSET Pagination for Large Tables:** OFFSET scans and discards rows. Use keyset/cursor pagination for tables with >10K rows.
591
- 4. **Missing Index on Foreign Keys:** Every foreign key column MUST have an index. Without it, DELETE on the parent table does a full scan on the child table.
592
- 5. **Functions on Indexed Columns:** `WHERE YEAR(date_col) = 2024` or `WHERE LOWER(email) = 'x'` destroys SARGability. Use range comparisons or functional indexes.
593
- 6. **Cursor Loops:** Never use `WHILE`/cursor loops to process rows individually. Use set-based operations (JOINs, CTEs, window functions).
594
- 7. **LAST_VALUE Without Frame Clause:** `LAST_VALUE() OVER (ORDER BY x)` returns the current row due to default frame. Must specify `ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING`.
595
- 8. **EXPLAIN Without ANALYZE:** `EXPLAIN` shows estimates. `EXPLAIN ANALYZE` shows actual execution. Always use ANALYZE for real performance data.
596
- 9. **Confusing PostgreSQL and SQL Server Syntax:** PostgreSQL uses `ON CONFLICT` for upserts, `||` for concatenation, `SERIAL` for auto-increment. SQL Server uses `MERGE`, `+` for concatenation, `IDENTITY`. Never mix them.
597
- 10. **Missing WHERE on UPDATE/DELETE:** Always double-check that UPDATE and DELETE statements have a WHERE clause. A missing WHERE updates/deletes ALL rows.
598
-
599
- ---
600
-
601
- ## 🏛️ Tribunal Integration (Anti-Hallucination)
602
-
603
- **Slash command: `/tribunal-database`**
604
- **Active reviewers: `logic` · `security` · `sql`**
605
-
606
- ### ❌ Forbidden AI Tropes
607
-
608
- 1. **Blind Assumptions:** Never assume table/column names. Always `// VERIFY: [check schema]`.
609
- 2. **Silent Degradation:** Writing queries that silently return wrong results due to implicit type casts.
610
- 3. **Context Amnesia:** Forgetting which database engine the project uses (PostgreSQL vs MySQL vs SQL Server).
611
- 4. **Over-Normalization:** Creating 10 tables where 3 with proper JSONB columns would suffice.
612
-
613
- ### ✅ Pre-Flight Self-Audit
614
-
615
- Review these questions before confirming output:
616
- ```
617
- ✅ Did I use specific column names (not SELECT *)?
618
- ✅ Is the query SARGable (no functions on indexed columns in WHERE)?
619
- ✅ Did I use parameterized queries (not string interpolation)?
620
- ✅ Did I add indexes for JOIN columns and foreign keys?
621
- ✅ Does UPDATE/DELETE have a proper WHERE clause?
622
- ✅ Did I use keyset pagination (not OFFSET) for large tables?
623
- ✅ Did I specify frame clauses for LAST_VALUE window functions?
624
- ✅ Did I use the correct syntax for the target database engine?
625
- ✅ Did I wrap EXPLAIN ANALYZE of destructive queries in BEGIN/ROLLBACK?
626
- ✅ Did I handle NULL properly (IS NULL, COALESCE, NULLIF)?
627
- ```
628
-
629
- ### 🛑 Verification-Before-Completion (VBC) Protocol
630
-
631
- **CRITICAL:** You must follow a strict "evidence-based closeout" state machine.
632
- - ❌ **Forbidden:** Declaring a query "optimized" or a migration "successful" without executing it.
633
- - ✅ **Required:** You are explicitly forbidden from completing your SQL task without providing **concrete terminal evidence** (e.g., `EXPLAIN ANALYZE` output, successful migration logs, or passing integration tests) proving the queries are syntactically valid and performant.
1
+ ---
2
+ name: sql-pro
3
+ description: Senior SQL developer across major databases (PostgreSQL, MySQL, SQL Server, Oracle). Complex query design with CTEs, window functions, PIVOT, recursive queries, JSON operations, full-text search, performance optimization with EXPLAIN ANALYZE, indexing strategies, partitioning, and schema architecture. Use when writing queries, designing schemas, optimizing performance, or debugging slow queries.
4
+ allowed-tools: Read, Write, Edit, Glob, Grep
5
+ version: 2.0.0
6
+ last-updated: 2026-03-30
7
+ applies-to-model: gemini-2.5-pro, claude-3-7-sonnet
8
+ ---
9
+
10
+ # SQL Pro — Advanced Query & Schema Mastery
11
+
12
+ ---
13
+
14
+ ## Common Table Expressions (CTEs)
15
+
16
+ ### Basic CTE
17
+
18
+ ```sql
19
+ -- CTE for readability and reuse
20
+ WITH active_users AS (
21
+ SELECT id, name, email, created_at
22
+ FROM users
23
+ WHERE is_active = true
24
+ AND last_login > CURRENT_DATE - INTERVAL '30 days'
25
+ ),
26
+ user_orders AS (
27
+ SELECT user_id, COUNT(*) AS order_count, SUM(total) AS total_spent
28
+ FROM orders
29
+ WHERE status = 'completed'
30
+ GROUP BY user_id
31
+ )
32
+ SELECT
33
+ u.name,
34
+ u.email,
35
+ COALESCE(o.order_count, 0) AS orders,
36
+ COALESCE(o.total_spent, 0) AS revenue
37
+ FROM active_users u
38
+ LEFT JOIN user_orders o ON u.id = o.user_id
39
+ ORDER BY revenue DESC;
40
+ ```
41
+
42
+ ### Recursive CTE (Hierarchical Data)
43
+
44
+ ```sql
45
+ -- Org chart: find all reports under a manager
46
+ WITH RECURSIVE org_tree AS (
47
+ -- Base case: the starting manager
48
+ SELECT id, name, manager_id, 1 AS depth
49
+ FROM employees
50
+ WHERE id = 42 -- starting point
51
+
52
+ UNION ALL
53
+
54
+ -- Recursive case: find direct reports
55
+ SELECT e.id, e.name, e.manager_id, t.depth + 1
56
+ FROM employees e
57
+ INNER JOIN org_tree t ON e.manager_id = t.id
58
+ WHERE t.depth < 10 -- safety limit to prevent infinite loops
59
+ )
60
+ SELECT * FROM org_tree ORDER BY depth, name;
61
+
62
+ -- ❌ HALLUCINATION TRAP: Always include a depth/cycle guard
63
+ -- Without it, circular references cause infinite recursion
64
+ -- PostgreSQL: use CYCLE detection clause (PG 14+)
65
+ -- SQL Server: use MAXRECURSION option
66
+ ```
67
+
68
+ ### CTE for Running Totals & Pagination
69
+
70
+ ```sql
71
+ -- Keyset pagination (faster than OFFSET for large tables)
72
+ WITH page AS (
73
+ SELECT id, name, created_at
74
+ FROM products
75
+ WHERE (created_at, id) < (:last_created_at, :last_id) -- cursor
76
+ ORDER BY created_at DESC, id DESC
77
+ LIMIT 20
78
+ )
79
+ SELECT * FROM page;
80
+
81
+ -- ❌ HALLUCINATION TRAP: OFFSET-based pagination gets slower with higher pages
82
+ -- OFFSET 100000, LIMIT 20 scans and discards 100,000 rows
83
+ -- Keyset pagination is O(1) regardless of page number
84
+ ```
85
+
86
+ ---
87
+
88
+ ## Window Functions
89
+
90
+ ### Ranking Functions
91
+
92
+ ```sql
93
+ -- ROW_NUMBER: unique sequential number per partition
94
+ SELECT
95
+ department,
96
+ name,
97
+ salary,
98
+ ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rank_in_dept
99
+ FROM employees;
100
+
101
+ -- RANK vs DENSE_RANK
102
+ -- RANK: 1, 2, 2, 4 (gaps after ties)
103
+ -- DENSE_RANK: 1, 2, 2, 3 (no gaps)
104
+ SELECT
105
+ name,
106
+ score,
107
+ RANK() OVER (ORDER BY score DESC) AS rank,
108
+ DENSE_RANK() OVER (ORDER BY score DESC) AS dense_rank
109
+ FROM leaderboard;
110
+
111
+ -- NTILE: divide into N equal groups
112
+ SELECT
113
+ name,
114
+ revenue,
115
+ NTILE(4) OVER (ORDER BY revenue DESC) AS quartile
116
+ FROM companies;
117
+ ```
118
+
119
+ ### Aggregate Windows
120
+
121
+ ```sql
122
+ -- Running total
123
+ SELECT
124
+ date,
125
+ amount,
126
+ SUM(amount) OVER (ORDER BY date) AS running_total,
127
+ AVG(amount) OVER (ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS moving_avg_7d
128
+ FROM daily_revenue;
129
+
130
+ -- Percentage of total
131
+ SELECT
132
+ category,
133
+ revenue,
134
+ ROUND(100.0 * revenue / SUM(revenue) OVER (), 2) AS pct_of_total
135
+ FROM category_sales;
136
+
137
+ -- Difference from previous row
138
+ SELECT
139
+ month,
140
+ revenue,
141
+ revenue - LAG(revenue) OVER (ORDER BY month) AS mom_change,
142
+ ROUND(100.0 * (revenue - LAG(revenue) OVER (ORDER BY month))
143
+ / NULLIF(LAG(revenue) OVER (ORDER BY month), 0), 2) AS mom_pct_change
144
+ FROM monthly_revenue;
145
+ ```
146
+
147
+ ### Frame Clauses
148
+
149
+ ```sql
150
+ -- Frame clause controls which rows the window function sees
151
+ SUM(amount) OVER (
152
+ ORDER BY date
153
+ ROWS BETWEEN 2 PRECEDING AND CURRENT ROW -- last 3 rows (physical)
154
+ )
155
+
156
+ SUM(amount) OVER (
157
+ ORDER BY date
158
+ RANGE BETWEEN INTERVAL '7 days' PRECEDING AND CURRENT ROW -- last 7 days (logical)
159
+ )
160
+
161
+ -- ROWS vs RANGE:
162
+ -- ROWS = physical row count (exact)
163
+ -- RANGE = logical value range (handles ties differently)
164
+
165
+ -- HALLUCINATION TRAP: Default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
166
+ -- This means SUM() OVER (ORDER BY x) includes ALL preceding rows, not just "the one before"
167
+ -- To get a true running count of N rows, use ROWS BETWEEN explicitly
168
+ ```
169
+
170
+ ### Lead / Lag Analysis
171
+
172
+ ```sql
173
+ -- Next and previous values
174
+ SELECT
175
+ event_date,
176
+ user_id,
177
+ LAG(event_date) OVER (PARTITION BY user_id ORDER BY event_date) AS prev_visit,
178
+ LEAD(event_date) OVER (PARTITION BY user_id ORDER BY event_date) AS next_visit,
179
+ event_date - LAG(event_date) OVER (PARTITION BY user_id ORDER BY event_date) AS days_between
180
+ FROM user_events;
181
+
182
+ -- FIRST_VALUE / LAST_VALUE
183
+ SELECT
184
+ department,
185
+ name,
186
+ salary,
187
+ FIRST_VALUE(name) OVER (PARTITION BY department ORDER BY salary DESC) AS highest_paid,
188
+ LAST_VALUE(name) OVER (
189
+ PARTITION BY department ORDER BY salary DESC
190
+ ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING -- ⚠️ required!
191
+ ) AS lowest_paid
192
+ FROM employees;
193
+
194
+ -- HALLUCINATION TRAP: LAST_VALUE without explicit frame clause returns CURRENT ROW
195
+ -- You MUST specify ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
196
+ ```
197
+
198
+ ---
199
+
200
+ ## PIVOT / UNPIVOT / Conditional Aggregation
201
+
202
+ ### PostgreSQL / Standard SQL (Conditional Aggregation)
203
+
204
+ ```sql
205
+ -- PostgreSQL doesn't have PIVOT use conditional aggregation
206
+ SELECT
207
+ product_name,
208
+ SUM(CASE WHEN quarter = 'Q1' THEN revenue ELSE 0 END) AS q1,
209
+ SUM(CASE WHEN quarter = 'Q2' THEN revenue ELSE 0 END) AS q2,
210
+ SUM(CASE WHEN quarter = 'Q3' THEN revenue ELSE 0 END) AS q3,
211
+ SUM(CASE WHEN quarter = 'Q4' THEN revenue ELSE 0 END) AS q4,
212
+ SUM(revenue) AS total
213
+ FROM quarterly_sales
214
+ GROUP BY product_name
215
+ ORDER BY total DESC;
216
+
217
+ -- PostgreSQL crosstab (requires tablefunc extension)
218
+ SELECT * FROM crosstab(
219
+ 'SELECT product, quarter, revenue FROM sales ORDER BY 1, 2',
220
+ 'SELECT DISTINCT quarter FROM sales ORDER BY 1'
221
+ ) AS ct(product TEXT, q1 NUMERIC, q2 NUMERIC, q3 NUMERIC, q4 NUMERIC);
222
+ ```
223
+
224
+ ### SQL Server PIVOT
225
+
226
+ ```sql
227
+ -- SQL Server native PIVOT
228
+ SELECT *
229
+ FROM (
230
+ SELECT product_name, quarter, revenue
231
+ FROM quarterly_sales
232
+ ) AS source
233
+ PIVOT (
234
+ SUM(revenue)
235
+ FOR quarter IN ([Q1], [Q2], [Q3], [Q4])
236
+ ) AS pivoted;
237
+ ```
238
+
239
+ ---
240
+
241
+ ## JSON Operations
242
+
243
+ ### PostgreSQL JSONB
244
+
245
+ ```sql
246
+ -- Query JSON fields
247
+ SELECT
248
+ id,
249
+ profile->>'name' AS name, -- text extraction
250
+ profile->'address'->>'city' AS city, -- nested extraction
251
+ (profile->>'age')::int AS age -- cast to int
252
+ FROM users
253
+ WHERE profile->>'country' = 'US'
254
+ AND (profile->>'age')::int >= 18;
255
+
256
+ -- JSONB containment
257
+ SELECT * FROM products
258
+ WHERE metadata @> '{"category": "electronics"}'; -- contains
259
+
260
+ -- JSONB existence
261
+ SELECT * FROM products
262
+ WHERE metadata ? 'warranty'; -- key exists
263
+
264
+ -- JSONB array queries
265
+ SELECT * FROM users
266
+ WHERE profile->'tags' ? 'premium'; -- array contains value
267
+
268
+ -- Update JSONB
269
+ UPDATE users
270
+ SET profile = jsonb_set(profile, '{address,city}', '"New York"')
271
+ WHERE id = 1;
272
+
273
+ -- JSONB aggregation
274
+ SELECT jsonb_agg(jsonb_build_object('id', id, 'name', name)) AS users_json
275
+ FROM users
276
+ WHERE is_active = true;
277
+
278
+ -- ❌ HALLUCINATION TRAP: -> returns JSON, ->> returns TEXT
279
+ -- Filtering on -> requires JSON comparison
280
+ -- Filtering on ->> allows string comparison
281
+ -- Always cast ->> results when comparing numbers: (col->>'age')::int
282
+ ```
283
+
284
+ ---
285
+
286
+ ## Indexing Strategy
287
+
288
+ ### Index Types
289
+
290
+ ```sql
291
+ -- B-tree (default — good for equality and range)
292
+ CREATE INDEX idx_users_email ON users (email);
293
+
294
+ -- Composite index (column order matters!)
295
+ CREATE INDEX idx_orders_user_date ON orders (user_id, created_at DESC);
296
+ -- ✅ Supports: WHERE user_id = 1 AND created_at > '2024-01-01'
297
+ -- Supports: WHERE user_id = 1 (uses leftmost prefix)
298
+ -- Does NOT support: WHERE created_at > '2024-01-01' (skips first column)
299
+
300
+ -- Partial / Filtered index (index only matching rows)
301
+ CREATE INDEX idx_active_users ON users (email) WHERE is_active = true;
302
+ -- Smaller index, faster queries when filtering by is_active
303
+
304
+ -- Covering index (INCLUDE avoids table lookup)
305
+ CREATE INDEX idx_orders_covering ON orders (user_id)
306
+ INCLUDE (total, status, created_at);
307
+ -- All needed columns in the index = index-only scan
308
+
309
+ -- GIN index (for JSONB, arrays, full-text search)
310
+ CREATE INDEX idx_products_metadata ON products USING gin (metadata);
311
+
312
+ -- GiST index (for geometric, range, full-text)
313
+ CREATE INDEX idx_locations_geo ON locations USING gist (coordinates);
314
+
315
+ -- BRIN index (for naturally ordered data like timestamps)
316
+ CREATE INDEX idx_logs_timestamp ON logs USING brin (created_at);
317
+ -- Tiny index, perfect for append-only tables with timestamp ordering
318
+ ```
319
+
320
+ ### SARGability
321
+
322
+ ```sql
323
+ -- SARGable = Search ARGument ABLE — can the query use an index?
324
+
325
+ -- ✅ SARGable (index seekable)
326
+ WHERE created_at >= '2024-01-01'
327
+ WHERE email = 'alice@test.com'
328
+ WHERE name LIKE 'Ali%' -- prefix match
329
+
330
+ -- NOT SARGable (forces full table scan)
331
+ WHERE YEAR(created_at) = 2024 -- function on column
332
+ WHERE LOWER(email) = 'alice@test.com' -- function on column
333
+ WHERE name LIKE '%alice%' -- leading wildcard
334
+ WHERE amount + tax > 100 -- expression on column
335
+ WHERE COALESCE(name, '') = '' -- function on column
336
+
337
+ -- Fix: functional index (PostgreSQL)
338
+ CREATE INDEX idx_users_email_lower ON users (LOWER(email));
339
+ -- Now WHERE LOWER(email) = 'alice@test.com' IS SARGable
340
+
341
+ -- Fix: computed column (SQL Server)
342
+ ALTER TABLE users ADD email_lower AS LOWER(email) PERSISTED;
343
+ CREATE INDEX idx_email_lower ON users (email_lower);
344
+
345
+ -- HALLUCINATION TRAP: Implicit type conversions destroy SARGability
346
+ -- WHERE varchar_column = 123 ← implicit cast on EVERY row
347
+ -- WHERE varchar_column = '123' ← direct comparison, uses index
348
+ ```
349
+
350
+ ---
351
+
352
+ ## EXPLAIN ANALYZE (Query Optimization)
353
+
354
+ ```sql
355
+ -- PostgreSQL
356
+ EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
357
+ SELECT * FROM orders WHERE user_id = 42 AND status = 'completed';
358
+
359
+ -- Reading the output:
360
+ -- Seq Scan → full table scan (usually bad for large tables)
361
+ -- Index Scan → using an index to find rows (good)
362
+ -- Index Only Scan → all data from index, no table access (best)
363
+ -- Bitmap Scan index + bitmap for multiple conditions (good for moderate selectivity)
364
+ -- Hash Join building hash table for join (good for large joins)
365
+ -- Nested Loop → for each row in A, scan B (good for small datasets, bad for large)
366
+ -- Sort explicit sorting (check if index can avoid this)
367
+
368
+ -- Key metrics:
369
+ -- actual time=X..Y X = time to first row, Y = time to all rows (ms)
370
+ -- rows=N → actual rows returned
371
+ -- loops=N → number of times this node executed
372
+ -- Buffers: shared hit=N pages read from cache (good)
373
+ -- Buffers: shared read=N pages read from disk (measure of I/O cost)
374
+
375
+ -- SQL Server
376
+ SET STATISTICS IO ON;
377
+ SET STATISTICS TIME ON;
378
+ SELECT * FROM orders WHERE user_id = 42;
379
+ -- Check: logical reads (from cache), physical reads (from disk)
380
+
381
+ -- HALLUCINATION TRAP: EXPLAIN without ANALYZE shows estimates, NOT actuals
382
+ -- Always use EXPLAIN ANALYZE for real performance data
383
+ -- But CAREFUL: ANALYZE actually EXECUTES the query
384
+ -- For destructive queries (DELETE, UPDATE), wrap in a transaction:
385
+ BEGIN;
386
+ EXPLAIN ANALYZE DELETE FROM users WHERE id = 1;
387
+ ROLLBACK; -- prevents actual deletion
388
+ ```
389
+
390
+ ---
391
+
392
+ ## Table Partitioning
393
+
394
+ ```sql
395
+ -- PostgreSQL range partitioning (ideal for time-series data)
396
+ CREATE TABLE events (
397
+ id BIGSERIAL,
398
+ event_type TEXT NOT NULL,
399
+ payload JSONB,
400
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
401
+ ) PARTITION BY RANGE (created_at);
402
+
403
+ -- Create monthly partitions
404
+ CREATE TABLE events_2024_01 PARTITION OF events
405
+ FOR VALUES FROM ('2024-01-01') TO ('2024-02-01');
406
+ CREATE TABLE events_2024_02 PARTITION OF events
407
+ FOR VALUES FROM ('2024-02-01') TO ('2024-03-01');
408
+
409
+ -- Queries automatically prune partitions:
410
+ -- SELECT * FROM events WHERE created_at >= '2024-02-01'
411
+ -- Only scans events_2024_02 and later — skips events_2024_01 entirely
412
+
413
+ -- List partitioning (for categorical data)
414
+ CREATE TABLE orders (
415
+ id SERIAL,
416
+ region TEXT NOT NULL,
417
+ total NUMERIC
418
+ ) PARTITION BY LIST (region);
419
+
420
+ CREATE TABLE orders_us PARTITION OF orders FOR VALUES IN ('US');
421
+ CREATE TABLE orders_eu PARTITION OF orders FOR VALUES IN ('EU', 'UK');
422
+ CREATE TABLE orders_apac PARTITION OF orders FOR VALUES IN ('JP', 'KR', 'AU');
423
+ ```
424
+
425
+ ---
426
+
427
+ ## Transactions & Concurrency
428
+
429
+ ```sql
430
+ -- Proper transaction pattern
431
+ BEGIN;
432
+
433
+ -- Lock the row for update (prevents concurrent modification)
434
+ SELECT * FROM accounts WHERE id = 1 FOR UPDATE;
435
+
436
+ UPDATE accounts SET balance = balance - 100 WHERE id = 1;
437
+ UPDATE accounts SET balance = balance + 100 WHERE id = 2;
438
+
439
+ -- Verify consistency
440
+ DO $$
441
+ BEGIN
442
+ IF (SELECT SUM(balance) FROM accounts) <> 10000 THEN
443
+ RAISE EXCEPTION 'Balance integrity violation';
444
+ END IF;
445
+ END $$;
446
+
447
+ COMMIT;
448
+
449
+ -- Isolation levels (ordered by strictness)
450
+ SET TRANSACTION ISOLATION LEVEL READ COMMITTED; -- default (PostgreSQL)
451
+ SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; -- snapshot isolation
452
+ SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; -- strictest (may abort)
453
+
454
+ -- Advisory locks (application-level locking)
455
+ SELECT pg_advisory_lock(12345); -- acquire
456
+ -- ... do work ...
457
+ SELECT pg_advisory_unlock(12345); -- release
458
+ ```
459
+
460
+ ---
461
+
462
+ ## MERGE / UPSERT
463
+
464
+ ```sql
465
+ -- PostgreSQL UPSERT (ON CONFLICT)
466
+ INSERT INTO products (sku, name, price, updated_at)
467
+ VALUES ('ABC-123', 'Widget', 29.99, NOW())
468
+ ON CONFLICT (sku) DO UPDATE SET
469
+ name = EXCLUDED.name,
470
+ price = EXCLUDED.price,
471
+ updated_at = EXCLUDED.updated_at;
472
+
473
+ -- SQL Server MERGE
474
+ MERGE INTO products AS target
475
+ USING staging_products AS source
476
+ ON target.sku = source.sku
477
+ WHEN MATCHED THEN
478
+ UPDATE SET
479
+ name = source.name,
480
+ price = source.price,
481
+ updated_at = GETDATE()
482
+ WHEN NOT MATCHED THEN
483
+ INSERT (sku, name, price, created_at)
484
+ VALUES (source.sku, source.name, source.price, GETDATE());
485
+
486
+ -- HALLUCINATION TRAP: PostgreSQL uses ON CONFLICT, not MERGE
487
+ -- MERGE was added in PostgreSQL 15+ but ON CONFLICT is idiomatic
488
+ -- SQL Server uses MERGE — do not confuse the two syntaxes
489
+ ```
490
+
491
+ ---
492
+
493
+ ## Full-Text Search (PostgreSQL)
494
+
495
+ ```sql
496
+ -- Create tsvector column and GIN index
497
+ ALTER TABLE articles ADD COLUMN search_vector tsvector;
498
+
499
+ UPDATE articles SET search_vector =
500
+ setweight(to_tsvector('english', COALESCE(title, '')), 'A') ||
501
+ setweight(to_tsvector('english', COALESCE(body, '')), 'B');
502
+
503
+ CREATE INDEX idx_articles_search ON articles USING gin (search_vector);
504
+
505
+ -- Search with ranking
506
+ SELECT
507
+ title,
508
+ ts_rank(search_vector, query) AS rank
509
+ FROM articles, to_tsquery('english', 'database & optimization') AS query
510
+ WHERE search_vector @@ query
511
+ ORDER BY rank DESC
512
+ LIMIT 20;
513
+
514
+ -- Trigger to auto-update search vector
515
+ CREATE FUNCTION update_search_vector() RETURNS trigger AS $$
516
+ BEGIN
517
+ NEW.search_vector :=
518
+ setweight(to_tsvector('english', COALESCE(NEW.title, '')), 'A') ||
519
+ setweight(to_tsvector('english', COALESCE(NEW.body, '')), 'B');
520
+ RETURN NEW;
521
+ END $$ LANGUAGE plpgsql;
522
+
523
+ CREATE TRIGGER trg_articles_search
524
+ BEFORE INSERT OR UPDATE OF title, body ON articles
525
+ FOR EACH ROW EXECUTE FUNCTION update_search_vector();
526
+ ```
527
+
528
+ ---
529
+
530
+ ## Security
531
+
532
+ ```sql
533
+ -- ✅ ALWAYS use parameterized queries
534
+ -- PostgreSQL (via psycopg)
535
+ cursor.execute("SELECT * FROM users WHERE email = %s", (email,))
536
+
537
+ -- Python SQLAlchemy
538
+ stmt = select(User).where(User.email == email)
539
+
540
+ -- NEVER: String interpolation for SQL
541
+ -- f"SELECT * FROM users WHERE email = '{email}'"
542
+ -- ❌ "SELECT * FROM users WHERE email = '" + email + "'"
543
+ -- These allow SQL injection: email = "'; DROP TABLE users; --"
544
+
545
+ -- Row-Level Security (PostgreSQL)
546
+ ALTER TABLE documents ENABLE ROW LEVEL SECURITY;
547
+
548
+ CREATE POLICY documents_owner_policy ON documents
549
+ USING (owner_id = current_setting('app.current_user_id')::int);
550
+
551
+ -- Grant minimum permissions
552
+ GRANT SELECT, INSERT ON users TO app_role;
553
+ -- ❌ NEVER: GRANT ALL ON DATABASE TO app_role
554
+ ```
555
+
556
+ ---
557
+
558
+ ## Output Format
559
+
560
+ When this skill produces or reviews code, structure your output as follows:
561
+
562
+ ```
563
+ ━━━ SQL Pro Report ━━━━━━━━━━━━━━━━━━━━━━━━
564
+ Skill: SQL Pro
565
+ Database: [PostgreSQL/MySQL/SQL Server/Oracle]
566
+ Scope: [N queries · N tables]
567
+ ─────────────────────────────────────────────────
568
+ ✅ Passed: [checks that passed, or "All clean"]
569
+ ⚠️ Warnings: [non-blocking issues, or "None"]
570
+ ❌ Blocked: [blocking issues requiring fix, or "None"]
571
+ ─────────────────────────────────────────────────
572
+ VBC status: PENDING VERIFIED
573
+ Evidence: [EXPLAIN ANALYZE output / migration success / test pass]
574
+ ```
575
+
576
+ **VBC (Verification-Before-Completion) is mandatory.**
577
+ Do not mark status as VERIFIED until concrete terminal evidence is provided.
578
+
579
+ ---