Skills.MD
# Query Builder
Convert natural language questions into SQL queries using the database schema.
## When to Use
Activate when user asks questions like:
- "Show me all users who signed up last month"
- "Find orders greater than $100"
- "Which products have low inventory?"
- "Get the top 10 customers by total spend"
## Workflow
### 1. Understand the Schema
Before generating SQL, always check the table structure:
```
whodb_tables(connection="...") → Get available tables
whodb_columns(table="relevant_table") → Get column names and types
```
### 2. Identify Intent
Parse the natural language request:
- **Subject**: What entity? (users, orders, products)
- **Filter**: What conditions? (last month, > $100, active)
- **Aggregation**: Count, sum, average, max, min?
- **Grouping**: By what dimension?
- **Ordering**: Sort by what? Ascending/descending?
- **Limit**: How many results?
### 3. Map to Schema
- Match entities to table names
- Match attributes to column names
- Identify foreign key joins needed
### 4. Generate SQL
Build the query following SQL best practices:
```sql
SELECT columns
FROM table
[JOIN other_table ON condition]
WHERE filters
[GROUP BY columns]
[HAVING aggregate_condition]
ORDER BY column [ASC|DESC]
LIMIT n;
```
### 5. Execute and Present
```
whodb_query(query="generated SQL")
```
## Translation Patterns
| Natural Language | SQL Pattern |
|------------------|-------------|
| "last week/month/year" | `WHERE date_col >= DATE_SUB(NOW(), INTERVAL 1 WEEK)` |
| "more than X" / "greater than X" | `WHERE col > X` |
| "top N" | `ORDER BY col DESC LIMIT N` |
| "how many" | `SELECT COUNT(*)` |
| "total" / "sum of" | `SELECT SUM(col)` |
| "average" | `SELECT AVG(col)` |
| "for each" / "by" | `GROUP BY col` |
| "between X and Y" | `WHERE col BETWEEN X AND Y` |
| "contains" / "like" | `WHERE col LIKE '%term%'` |
| "starts with" | `WHERE col LIKE 'term%'` |
| "is empty" / "is null" | `WHERE col IS NULL` |
| "is not empty" | `WHERE col IS NOT NULL` |
## Date Handling by Database
### PostgreSQL
```sql
WHERE created_at >= NOW() - INTERVAL '7 days'
WHERE created_at >= DATE_TRUNC('month', CURRENT_DATE)
```
### MySQL
```sql
WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)
WHERE created_at >= DATE_FORMAT(NOW(), '%Y-%m-01')
```
### SQLite
```sql
WHERE created_at >= DATE('now', '-7 days')
WHERE created_at >= DATE('now', 'start of month')
```
## Examples
### "Show me users who signed up this month"
```sql
SELECT * FROM users
WHERE created_at >= DATE_TRUNC('month', CURRENT_DATE)
ORDER BY created_at DESC;
```
### "Find the top 5 products by sales"
```sql
SELECT p.name, SUM(oi.quantity) as total_sold
FROM products p
JOIN order_items oi ON p.id = oi.product_id
GROUP BY p.id, p.name
ORDER BY total_sold DESC
LIMIT 5;
```
### "How many orders per customer?"
```sql
SELECT customer_id, COUNT(*) as order_count
FROM orders
GROUP BY customer_id
ORDER BY order_count DESC;
```
## Safety Rules
- Always use LIMIT for exploratory queries (default: 100)
- Never generate DELETE, UPDATE, or DROP unless explicitly requested
- Warn if query might return large result sets
- Use table aliases for readability in JOINs
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「query-builder」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次