home / skills / openclaw / skills / ai-sql

This skill converts natural language into optimized SQL queries across dialects, handling joins and conditions to accelerate data retrieval.

npx playbooks add skill openclaw/skills --skill ai-sql

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
1.2 KB
---
name: sql-gen
description: Generate SQL queries from natural language
---

# SQL Generator

Describe what you want, get the SQL. Works with any database.

## Quick Start

```bash
npx ai-sql "get all users who signed up this month and made a purchase"
```

## What It Does

- Converts English to SQL
- Handles complex JOINs
- Adds proper WHERE clauses
- Optimizes for performance

## Usage Examples

```bash
# Generate query
npx ai-sql "top 10 products by revenue last quarter"

# With schema context
npx ai-sql "users without orders" --schema ./schema.sql

# Specific dialect
npx ai-sql "monthly active users" --dialect postgres
```

## Dialects Supported

- PostgreSQL
- MySQL
- SQLite
- SQL Server
- Oracle

## Output Example

```sql
SELECT u.id, u.email, COUNT(o.id) as order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.created_at >= DATE_TRUNC('month', CURRENT_DATE)
GROUP BY u.id, u.email
HAVING COUNT(o.id) > 0;
```

## Requirements

Node.js 18+. OPENAI_API_KEY required.

## License

MIT. Free forever.

---

**Built by LXGIC Studios**

- GitHub: [github.com/lxgicstudios/ai-sql](https://github.com/lxgicstudios/ai-sql)
- Twitter: [@lxgicstudios](https://x.com/lxgicstudios)

Overview

This skill turns plain English prompts into runnable SQL queries for a wide range of SQL dialects. It helps developers and analysts rapidly prototype queries, handle joins and aggregations, and tailor output to a target database dialect. Use it to save time and reduce manual query errors.

How this skill works

You provide a natural language description of the data you need and optionally supply a schema file or select a dialect. The skill parses intent, maps terms to tables and columns, constructs JOINs, WHERE clauses, GROUP BY and HAVING as needed, and outputs an optimized SQL statement. It supports dialect-specific syntax and can prioritize performance-friendly constructs.

When to use it

  • Exploratory data analysis when you need quick, accurate SQL without manual drafting
  • Generating complex JOINs and aggregated queries from a brief description
  • Translating business questions into queries for different SQL dialects
  • Prototyping reports or dashboards before integrating into code
  • Onboarding new team members who are not fluent in SQL syntax

Best practices

  • Provide schema context (DDL or sample table names) to improve column resolution
  • Specify the target dialect (Postgres, MySQL, SQLite, SQL Server, Oracle) for correct syntax
  • Include filtering details like date ranges, status values, or sampling rules to narrow results
  • Validate generated queries in a safe dev environment before running on production
  • Refine prompts iteratively—start broad, then add constraints or indexes to optimize

Example use cases

  • Produce a query for top 10 products by revenue last quarter, with GROUP BY and ORDER BY
  • Find users who signed up this month and made at least one purchase using LEFT JOINs
  • List users without orders by comparing users and orders tables with an anti-join
  • Create a monthly active users query optimized for a specific SQL dialect
  • Convert analytics questions into SQL to embed in BI dashboards or scripts

FAQ

Which SQL dialects are supported?

Common dialects are supported including PostgreSQL, MySQL, SQLite, SQL Server, and Oracle.

Can I provide a schema so the skill uses real column names?

Yes. Supplying a schema file or DDL improves accuracy by allowing the generator to map natural language to actual tables and columns.

Is the generated SQL production-ready?

Generated SQL is intended to be syntactically correct and performance-conscious, but you should validate and test queries in your environment before production use.