skill v1.0.0

SQL Query Helper

Write, optimize, and explain SQL queries across PostgreSQL, MySQL, and SQLite

Categorydata
---
name: SQL Query Helper
description: Write, optimize, and explain SQL queries across PostgreSQL, MySQL, and SQLite
version: 1.0.0
author: chvor
type: workflow
category: data
icon: database
tags:
  - sql
  - database
  - postgresql
  - mysql
  - sqlite
  - queries
  - optimization
  - joins
  - window-functions
---
When the user asks for help with SQL:

## Modes

### Writing queries
1. Ask which database engine (Postgres, MySQL, SQLite) if not obvious
2. Clarify the data model — ask for table schemas or let them describe the structure
3. Write the query with comments explaining each section
4. Note any assumptions about the schema

### Explaining queries
1. Break the query into logical sections (FROM, JOIN, WHERE, GROUP BY, etc.)
2. Explain each section in plain English
3. Describe the data flow: "Start with all orders, join to customers, filter to last 30 days, group by region"

### Optimizing queries
1. Identify the bottleneck: missing indexes, full table scans, N+1 patterns, unnecessary subqueries
2. Suggest specific improvements with before/after examples
3. Recommend indexes if applicable: `CREATE INDEX idx_name ON table(column)`
4. Note trade-offs (e.g., denormalization speeds reads but complicates writes)

## Patterns to know

| Pattern | When to use |
|---------|-------------|
| **CTE (WITH)** | Break complex queries into readable steps |
| **Window functions** | Running totals, rankings, row comparisons without GROUP BY |
| **LATERAL JOIN** | Per-row subqueries (Po