tribunal-kit 4.5.0 → 4.6.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 (217) hide show
  1. package/.agent/.shared/ui-ux-pro-max/README.md +4 -4
  2. package/.agent/ARCHITECTURE.md +279 -277
  3. package/.agent/GEMINI.md +127 -121
  4. package/.agent/agents/accessibility-reviewer.md +187 -187
  5. package/.agent/agents/ai-code-reviewer.md +199 -199
  6. package/.agent/agents/api-architect.md +71 -66
  7. package/.agent/agents/backend-specialist.md +219 -215
  8. package/.agent/agents/cloud-engineer.md +98 -0
  9. package/.agent/agents/code-archaeologist.md +168 -161
  10. package/.agent/agents/database-architect.md +184 -184
  11. package/.agent/agents/db-latency-auditor.md +213 -216
  12. package/.agent/agents/debugger.md +198 -191
  13. package/.agent/agents/dependency-reviewer.md +106 -103
  14. package/.agent/agents/devops-engineer.md +218 -218
  15. package/.agent/agents/documentation-writer.md +209 -201
  16. package/.agent/agents/explorer-agent.md +167 -160
  17. package/.agent/agents/frontend-reviewer.md +162 -160
  18. package/.agent/agents/frontend-specialist.md +257 -248
  19. package/.agent/agents/game-developer.md +48 -48
  20. package/.agent/agents/logic-reviewer.md +118 -116
  21. package/.agent/agents/mobile-developer.md +197 -200
  22. package/.agent/agents/mobile-reviewer.md +159 -162
  23. package/.agent/agents/orchestrator.md +187 -181
  24. package/.agent/agents/penetration-tester.md +160 -157
  25. package/.agent/agents/performance-optimizer.md +183 -183
  26. package/.agent/agents/performance-reviewer.md +178 -178
  27. package/.agent/agents/precedence-reviewer.md +251 -250
  28. package/.agent/agents/product-manager.md +149 -142
  29. package/.agent/agents/product-owner.md +81 -80
  30. package/.agent/agents/project-planner.md +152 -142
  31. package/.agent/agents/qa-automation-engineer.md +216 -225
  32. package/.agent/agents/resilience-reviewer.md +88 -88
  33. package/.agent/agents/schema-reviewer.md +67 -67
  34. package/.agent/agents/security-auditor.md +180 -174
  35. package/.agent/agents/seo-specialist.md +188 -193
  36. package/.agent/agents/sql-reviewer.md +159 -161
  37. package/.agent/agents/supervisor-agent.md +173 -184
  38. package/.agent/agents/swarm-worker-contracts.md +170 -166
  39. package/.agent/agents/swarm-worker-registry.md +92 -92
  40. package/.agent/agents/system-architect.md +85 -0
  41. package/.agent/agents/test-coverage-reviewer.md +158 -160
  42. package/.agent/agents/test-engineer.md +118 -118
  43. package/.agent/agents/throughput-optimizer.md +291 -299
  44. package/.agent/agents/type-safety-reviewer.md +182 -175
  45. package/.agent/agents/ui-ux-auditor.md +300 -292
  46. package/.agent/agents/vitals-reviewer.md +223 -223
  47. package/.agent/mcp_config.json +37 -40
  48. package/.agent/patterns/generator.md +11 -9
  49. package/.agent/patterns/inversion.md +14 -12
  50. package/.agent/patterns/pipeline.md +11 -9
  51. package/.agent/patterns/reviewer.md +15 -13
  52. package/.agent/patterns/tool-wrapper.md +11 -9
  53. package/.agent/routing_index.json +654 -0
  54. package/.agent/rules/GEMINI.md +358 -352
  55. package/.agent/scripts/compile_router.py +112 -0
  56. package/.agent/scripts/migrate_skills_frontmatter.py +64 -0
  57. package/.agent/scripts/strengthen_skills.js +1 -1
  58. package/.agent/skills/advanced-rag-pipelines/SKILL.md +56 -0
  59. package/.agent/skills/agent-organizer/SKILL.md +156 -150
  60. package/.agent/skills/agentic-patterns/SKILL.md +313 -315
  61. package/.agent/skills/ai-prompt-injection-defense/SKILL.md +190 -184
  62. package/.agent/skills/api-patterns/SKILL.md +253 -247
  63. package/.agent/skills/api-security-auditor/SKILL.md +195 -193
  64. package/.agent/skills/app-builder/SKILL.md +573 -572
  65. package/.agent/skills/app-builder/templates/SKILL.md +108 -115
  66. package/.agent/skills/app-builder/templates/astro-static/TEMPLATE.md +76 -76
  67. package/.agent/skills/app-builder/templates/chrome-extension/TEMPLATE.md +92 -92
  68. package/.agent/skills/app-builder/templates/cli-tool/TEMPLATE.md +88 -88
  69. package/.agent/skills/app-builder/templates/electron-desktop/TEMPLATE.md +88 -88
  70. package/.agent/skills/app-builder/templates/express-api/TEMPLATE.md +83 -83
  71. package/.agent/skills/app-builder/templates/flutter-app/TEMPLATE.md +90 -90
  72. package/.agent/skills/app-builder/templates/monorepo-turborepo/TEMPLATE.md +90 -90
  73. package/.agent/skills/app-builder/templates/nextjs-fullstack/TEMPLATE.md +126 -122
  74. package/.agent/skills/app-builder/templates/nextjs-saas/TEMPLATE.md +127 -122
  75. package/.agent/skills/app-builder/templates/nextjs-static/TEMPLATE.md +172 -169
  76. package/.agent/skills/app-builder/templates/nuxt-app/TEMPLATE.md +139 -134
  77. package/.agent/skills/app-builder/templates/python-fastapi/TEMPLATE.md +83 -83
  78. package/.agent/skills/app-builder/templates/react-native-app/TEMPLATE.md +122 -119
  79. package/.agent/skills/appflow-wireframe/SKILL.md +146 -145
  80. package/.agent/skills/architecture/SKILL.md +226 -219
  81. package/.agent/skills/authentication-best-practices/SKILL.md +197 -189
  82. package/.agent/skills/backend-security-expert/SKILL.md +16 -2
  83. package/.agent/skills/bash-linux/SKILL.md +179 -179
  84. package/.agent/skills/behavioral-modes/SKILL.md +239 -223
  85. package/.agent/skills/brainstorming/SKILL.md +498 -486
  86. package/.agent/skills/browser-native-ai/SKILL.md +57 -4
  87. package/.agent/skills/building-native-ui/SKILL.md +202 -202
  88. package/.agent/skills/cicd-pro/SKILL.md +442 -0
  89. package/.agent/skills/clean-code/SKILL.md +400 -381
  90. package/.agent/skills/cloud-architect/SKILL.md +439 -0
  91. package/.agent/skills/code-review-checklist/SKILL.md +203 -194
  92. package/.agent/skills/config-validator/SKILL.md +165 -165
  93. package/.agent/skills/containerization-pro/SKILL.md +452 -0
  94. package/.agent/skills/csharp-developer/SKILL.md +518 -518
  95. package/.agent/skills/data-validation-schemas/SKILL.md +333 -328
  96. package/.agent/skills/database-design/SKILL.md +247 -240
  97. package/.agent/skills/deployment-procedures/SKILL.md +172 -169
  98. package/.agent/skills/devops-engineer/SKILL.md +345 -345
  99. package/.agent/skills/devops-incident-responder/SKILL.md +143 -137
  100. package/.agent/skills/doc.md +209 -177
  101. package/.agent/skills/documentation-templates/SKILL.md +291 -279
  102. package/.agent/skills/edge-computing/SKILL.md +183 -181
  103. package/.agent/skills/error-resilience/SKILL.md +411 -428
  104. package/.agent/skills/extract-design-system/SKILL.md +160 -158
  105. package/.agent/skills/framer-motion-expert/SKILL.md +253 -244
  106. package/.agent/skills/frontend-design/SKILL.md +208 -201
  107. package/.agent/skills/frontend-security-expert/SKILL.md +16 -3
  108. package/.agent/skills/game-design-expert/SKILL.md +132 -129
  109. package/.agent/skills/game-engineering-expert/SKILL.md +148 -146
  110. package/.agent/skills/generative-ui-expert/SKILL.md +57 -1
  111. package/.agent/skills/geo-fundamentals/SKILL.md +148 -147
  112. package/.agent/skills/git-pro/SKILL.md +435 -0
  113. package/.agent/skills/github-operations/SKILL.md +335 -329
  114. package/.agent/skills/gsap-core/SKILL.md +319 -308
  115. package/.agent/skills/gsap-frameworks/SKILL.md +213 -207
  116. package/.agent/skills/gsap-performance/SKILL.md +139 -133
  117. package/.agent/skills/gsap-plugins/SKILL.md +486 -480
  118. package/.agent/skills/gsap-react/SKILL.md +202 -189
  119. package/.agent/skills/gsap-scrolltrigger/SKILL.md +357 -350
  120. package/.agent/skills/gsap-timeline/SKILL.md +165 -161
  121. package/.agent/skills/gsap-utils/SKILL.md +344 -338
  122. package/.agent/skills/harness-protocol/SKILL.md +48 -0
  123. package/.agent/skills/i18n-localization/SKILL.md +174 -163
  124. package/.agent/skills/intelligent-routing/SKILL.md +202 -246
  125. package/.agent/skills/knowledge-graph/SKILL.md +60 -52
  126. package/.agent/skills/lint-and-validate/SKILL.md +261 -261
  127. package/.agent/skills/llm-engineering/SKILL.md +400 -394
  128. package/.agent/skills/local-first/SKILL.md +178 -178
  129. package/.agent/skills/mcp-builder/SKILL.md +143 -142
  130. package/.agent/skills/mobile-design/SKILL.md +272 -263
  131. package/.agent/skills/monorepo-management/SKILL.md +335 -334
  132. package/.agent/skills/motion-engineering/SKILL.md +266 -234
  133. package/.agent/skills/nextjs-react-expert/SKILL.md +236 -234
  134. package/.agent/skills/nodejs-best-practices/SKILL.md +547 -548
  135. package/.agent/skills/observability/SKILL.md +343 -343
  136. package/.agent/skills/parallel-agents/SKILL.md +143 -146
  137. package/.agent/skills/performance-profiling/SKILL.md +259 -267
  138. package/.agent/skills/plan-writing/SKILL.md +150 -142
  139. package/.agent/skills/platform-engineer/SKILL.md +148 -147
  140. package/.agent/skills/playwright-best-practices/SKILL.md +188 -187
  141. package/.agent/skills/powershell-windows/SKILL.md +162 -162
  142. package/.agent/skills/project-idioms/SKILL.md +137 -137
  143. package/.agent/skills/python-patterns/SKILL.md +260 -259
  144. package/.agent/skills/python-pro/SKILL.md +324 -323
  145. package/.agent/skills/react-specialist/SKILL.md +305 -277
  146. package/.agent/skills/readme-builder/SKILL.md +310 -300
  147. package/.agent/skills/realtime-patterns/SKILL.md +323 -319
  148. package/.agent/skills/red-team-tactics/SKILL.md +231 -218
  149. package/.agent/skills/rust-pro/SKILL.md +671 -673
  150. package/.agent/skills/seo-fundamentals/SKILL.md +179 -179
  151. package/.agent/skills/server-management/SKILL.md +218 -214
  152. package/.agent/skills/shadcn-ui-expert/SKILL.md +231 -231
  153. package/.agent/skills/skill-creator/SKILL.md +87 -86
  154. package/.agent/skills/sql-pro/SKILL.md +629 -629
  155. package/.agent/skills/supabase-postgres-best-practices/SKILL.md +97 -97
  156. package/.agent/skills/swiftui-expert/SKILL.md +204 -201
  157. package/.agent/skills/system-design-pro/SKILL.md +345 -0
  158. package/.agent/skills/systematic-debugging/SKILL.md +153 -142
  159. package/.agent/skills/tailwind-patterns/SKILL.md +610 -566
  160. package/.agent/skills/tdd-workflow/SKILL.md +169 -161
  161. package/.agent/skills/test-result-analyzer/SKILL.md +313 -309
  162. package/.agent/skills/testing-patterns/SKILL.md +566 -579
  163. package/.agent/skills/trend-researcher/SKILL.md +243 -237
  164. package/.agent/skills/typescript-advanced/SKILL.md +336 -335
  165. package/.agent/skills/ui-ux-pro-max/SKILL.md +590 -562
  166. package/.agent/skills/ui-ux-researcher/SKILL.md +244 -244
  167. package/.agent/skills/vue-expert/SKILL.md +294 -275
  168. package/.agent/skills/vulnerability-scanner/SKILL.md +416 -404
  169. package/.agent/skills/web-accessibility-auditor/SKILL.md +219 -218
  170. package/.agent/skills/web-design-guidelines/SKILL.md +192 -186
  171. package/.agent/skills/webapp-testing/SKILL.md +167 -169
  172. package/.agent/skills/webgpu-performance/SKILL.md +56 -2
  173. package/.agent/skills/whimsy-injector/SKILL.md +346 -325
  174. package/.agent/skills/workflow-optimizer/SKILL.md +231 -229
  175. package/.agent/workflows/acf.md +141 -0
  176. package/.agent/workflows/api-tester.md +176 -151
  177. package/.agent/workflows/audit.md +150 -127
  178. package/.agent/workflows/brainstorm.md +134 -110
  179. package/.agent/workflows/changelog.md +140 -112
  180. package/.agent/workflows/create.md +168 -124
  181. package/.agent/workflows/debug.md +190 -165
  182. package/.agent/workflows/deploy.md +201 -180
  183. package/.agent/workflows/enhance.md +154 -128
  184. package/.agent/workflows/fix.md +136 -114
  185. package/.agent/workflows/generate.md +198 -183
  186. package/.agent/workflows/marathon.md +37 -11
  187. package/.agent/workflows/migrate.md +184 -160
  188. package/.agent/workflows/orchestrate.md +192 -168
  189. package/.agent/workflows/performance-benchmarker.md +135 -114
  190. package/.agent/workflows/plan.md +196 -173
  191. package/.agent/workflows/preview.md +103 -80
  192. package/.agent/workflows/refactor.md +192 -161
  193. package/.agent/workflows/review-ai.md +125 -101
  194. package/.agent/workflows/review.md +141 -116
  195. package/.agent/workflows/session.md +122 -94
  196. package/.agent/workflows/status.md +101 -79
  197. package/.agent/workflows/strengthen-skills.md +164 -138
  198. package/.agent/workflows/super-prompt.md +24 -0
  199. package/.agent/workflows/swarm.md +193 -179
  200. package/.agent/workflows/test.md +211 -189
  201. package/.agent/workflows/tribunal-backend.md +136 -105
  202. package/.agent/workflows/tribunal-database.md +122 -95
  203. package/.agent/workflows/tribunal-frontend.md +221 -96
  204. package/.agent/workflows/tribunal-full.md +129 -100
  205. package/.agent/workflows/tribunal-mobile.md +122 -95
  206. package/.agent/workflows/tribunal-performance.md +136 -110
  207. package/.agent/workflows/tribunal-speed.md +209 -183
  208. package/.agent/workflows/ui-ux-pro-max.md +145 -122
  209. package/README.md +107 -55
  210. package/bin/mcp-server.js +159 -0
  211. package/bin/tribunal-kit.js +105 -29
  212. package/bin/wrapper.js +16 -7
  213. package/mcp_config.json +9 -0
  214. package/package.json +94 -86
  215. package/scripts/changelog.js +4 -3
  216. package/scripts/validate-payload.js +6 -1
  217. package/scripts/postinstall.js +0 -127
@@ -1,633 +1,631 @@
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
- ---
580
-
581
-
582
- ---
583
-
584
-
585
-
586
- AI coding assistants often fall into specific bad habits when dealing with this domain. These are strictly forbidden:
587
-
588
- 1. **Over-engineering:** Proposing complex abstractions or distributed systems when a simpler approach suffices.
589
- 2. **Hallucinated Libraries/Methods:** Using non-existent methods or packages. Always `// VERIFY` or check `package.json` / `requirements.txt`.
590
- 3. **Skipping Edge Cases:** Writing the "happy path" and ignoring error handling, timeouts, or data validation.
591
- 4. **Context Amnesia:** Forgetting the user's constraints and offering generic advice instead of tailored solutions.
592
- 5. **Silent Degradation:** Catching and suppressing errors without logging or re-raising.
593
-
594
- ---
595
-
596
-
597
-
598
- **Slash command: `/review` or `/tribunal-full`**
599
- **Active reviewers: `logic-reviewer` · `security-auditor`**
600
-
601
- ### ❌ Forbidden AI Tropes
602
-
603
- 1. **Blind Assumptions:** Never make an assumption without documenting it clearly with `// VERIFY: [reason]`.
604
- 2. **Silent Degradation:** Catching and suppressing errors without logging or handling.
605
- 3. **Context Amnesia:** Forgetting the user's constraints and offering generic advice instead of tailored solutions.
606
-
607
-
608
-
609
- Review these questions before confirming output:
610
- ```
611
- ✅ Did I rely ONLY on real, verified tools and methods?
612
- ✅ Is this solution appropriately scoped to the user's constraints?
613
- ✅ Did I handle potential failure modes and edge cases?
614
- ✅ Have I avoided generic boilerplate that doesn't add value?
615
- ```
616
-
617
- ### 🛑 Verification-Before-Completion (VBC) Protocol
618
-
619
- **CRITICAL:** You must follow a strict "evidence-based closeout" state machine.
620
- - ❌ **Forbidden:** Declaring a task complete because the output "looks correct."
621
- - ✅ **Required:** You are explicitly forbidden from finalizing any task without providing **concrete evidence** (terminal output, passing tests, compile success, or equivalent proof) that your output works as intended.
622
-
623
-
624
- ## Pre-Flight Checklist
625
- - [ ] Have I reviewed the user's specific constraints and requests?
626
- - [ ] Have I checked the environment for relevant existing implementations?
627
-
628
- ## VBC Protocol (Verification-Before-Completion)
629
- You MUST verify existing code signatures and variables before attempting to modify or call them. No hallucination is permitted.
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
+ routing:
9
+ domain: general
10
+ tier: basic
11
+ ---
630
12
 
13
+ # SQL Pro — Advanced Query & Schema Mastery
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
+ ---
585
+
586
+ AI coding assistants often fall into specific bad habits when dealing with this domain. These are strictly forbidden:
587
+
588
+ 1. **Over-engineering:** Proposing complex abstractions or distributed systems when a simpler approach suffices.
589
+ 2. **Hallucinated Libraries/Methods:** Using non-existent methods or packages. Always `// VERIFY` or check `package.json` / `requirements.txt`.
590
+ 3. **Skipping Edge Cases:** Writing the "happy path" and ignoring error handling, timeouts, or data validation.
591
+ 4. **Context Amnesia:** Forgetting the user's constraints and offering generic advice instead of tailored solutions.
592
+ 5. **Silent Degradation:** Catching and suppressing errors without logging or re-raising.
593
+
594
+ ---
595
+
596
+ **Slash command: `/review` or `/tribunal-full`**
597
+ **Active reviewers: `logic-reviewer` · `security-auditor`**
598
+
599
+ ### ❌ Forbidden AI Tropes
600
+
601
+ 1. **Blind Assumptions:** Never make an assumption without documenting it clearly with `// VERIFY: [reason]`.
602
+ 2. **Silent Degradation:** Catching and suppressing errors without logging or handling.
603
+ 3. **Context Amnesia:** Forgetting the user's constraints and offering generic advice instead of tailored solutions.
604
+
605
+ Review these questions before confirming output:
606
+
607
+ ```
608
+ ✅ Did I rely ONLY on real, verified tools and methods?
609
+ ✅ Is this solution appropriately scoped to the user's constraints?
610
+ ✅ Did I handle potential failure modes and edge cases?
611
+ ✅ Have I avoided generic boilerplate that doesn't add value?
612
+ ```
613
+
614
+ ### 🛑 Verification-Before-Completion (VBC) Protocol
615
+
616
+ **CRITICAL:** You must follow a strict "evidence-based closeout" state machine.
617
+
618
+ - ❌ **Forbidden:** Declaring a task complete because the output "looks correct."
619
+ - ✅ **Required:** You are explicitly forbidden from finalizing any task without providing **concrete evidence** (terminal output, passing tests, compile success, or equivalent proof) that your output works as intended.
620
+
621
+ ## Pre-Flight Checklist
622
+
623
+ - [ ] Have I reviewed the user's specific constraints and requests?
624
+ - [ ] Have I checked the environment for relevant existing implementations?
625
+
626
+ ## VBC Protocol (Verification-Before-Completion)
627
+
628
+ You MUST verify existing code signatures and variables before attempting to modify or call them. No hallucination is permitted.
631
629
 
632
630
  ---
633
631
 
@@ -657,6 +655,7 @@ AI coding assistants often fall into specific bad habits when dealing with this
657
655
  ### ✅ Pre-Flight Self-Audit
658
656
 
659
657
  Review these questions before confirming output:
658
+
660
659
  ```
661
660
  ✅ Did I rely ONLY on real, verified tools and methods?
662
661
  ✅ Is this solution appropriately scoped to the user's constraints?
@@ -667,5 +666,6 @@ Review these questions before confirming output:
667
666
  ### 🛑 Verification-Before-Completion (VBC) Protocol
668
667
 
669
668
  **CRITICAL:** You must follow a strict "evidence-based closeout" state machine.
669
+
670
670
  - ❌ **Forbidden:** Declaring a task complete because the output "looks correct."
671
671
  - ✅ **Required:** You are explicitly forbidden from finalizing any task without providing **concrete evidence** (terminal output, passing tests, compile success, or equivalent proof) that your output works as intended.