Skill Definition

Copy the following content into .autohand/skills/data-querying/SKILL.md:

---
name: data-querying
description: Query databases and extract insights from data. Use for writing SQL, exploring schemas, generating reports, and answering business questions with data.
allowed-tools: run_command read_file write_file sql_query
---

# Skill: Data querying

## Purpose

Translate natural language questions into precise database queries, explore and document schemas, and extract insights. This skill operates with read-only access by default and emphasizes query performance and safety.

## When to use this skill

- Answering **business questions** that require querying production data.
- Exploring **database schemas** and understanding table relationships.
- Writing **complex SQL queries** with joins, aggregations, and CTEs.
- Generating **reports and dashboards** from raw data.
- Optimizing **slow queries** with better indexes or structure.

## Inputs

- **Natural language question**: the business question to answer.
- **Database context**: connection string, schema documentation, table relationships.
- **Access permissions**: which tables and columns are accessible.
- **Output format**: raw results, CSV export, chart-ready data.

## Out of scope

- Running **DELETE, UPDATE, or DROP** statements without explicit approval.
- Accessing **PII or sensitive columns** without proper authorization.
- Queries that could **lock tables or degrade performance** in production.
- Direct access to **production databases** when read replicas are available.

## Conventions

- Always use **parameterized queries** to prevent SQL injection.
- Add **LIMIT clauses** to prevent returning millions of rows.
- Use **CTEs over nested subqueries** for readability.
- Include **WHERE clauses** to scope queries appropriately.
- Format SQL with **consistent indentation and aliases**.

## Required behavior

1. Explore the schema to understand **tables, columns, and relationships**.
2. Write clear, readable SQL with **meaningful aliases**.
3. Verify query logic by **checking sample results** first.
4. Add appropriate **indexes or query hints** for performance.
5. Format results in a **clear, actionable way**.
6. Document any **assumptions or limitations** in the analysis.

## Required artifacts

- **SQL query file**: well-formatted query with comments.
- **Results summary**: key findings and insights.
- **Query explanation**: how the query answers the question.
- **Performance notes**: expected runtime, indexes used.

## Implementation checklist

1. Understand the question and clarify any ambiguity.
2. Explore the schema to identify relevant tables.
3. Draft the query with appropriate joins and filters.
4. Test with LIMIT to verify logic before full execution.
5. Check query performance with EXPLAIN.
6. Format and document the final query.
7. Present results with clear insights.

## Verification

Before delivering results:

- Verify the query **answers the original question**.
- Check for **edge cases and NULL handling**.
- Confirm results are **reasonable and expected**.
- Validate **performance is acceptable** for the data volume.

The skill is complete when:

- The business question is answered with data.
- Query is documented and reproducible.
- Results are formatted for the intended audience.
- Any limitations or caveats are noted.

## Safety and escalation

- If the question involves **PII or sensitive data**, verify authorization first.
- If a query could **impact production performance**, run on a read replica.
- If asked to run **write operations**, get explicit confirmation.
- If results seem **unexpected or anomalous**, flag for human review.
- Never expose **raw credentials or connection strings** in output.

Example usage

# Ask a question in plain English
autohand --skill data-querying --prompt "How many users signed up last month by country?"

# Explore the schema
autohand --skill data-querying --prompt "Show me all tables related to orders and their relationships"

# Generate a report
autohand --skill data-querying --prompt "Create a monthly revenue report broken down by product category"

# Optimize a query
autohand --skill data-querying --prompt "This query is slow, help me optimize it: SELECT * FROM orders WHERE..."

# Complex analysis
autohand --skill data-querying --prompt "Find users who made a purchase in the first week but never returned"