# How to Generate SQL Queries from Natural Language Descriptions Using pm-skills

> Easily generate SQL queries from natural language using pm-skills. Learn how this tool converts plain-English requirements into optimized SQL statements in four simple steps.

- Repository: [Pawel Huryn/pm-skills](https://github.com/phuryn/pm-skills)
- Tags: how-to-guide
- Published: 2026-06-13

---

**The pm-skills repository provides a SQL Query Generator skill that converts plain-English requirements into optimized, dialect-specific SQL statements through a four-step pipeline involving schema ingestion, request interpretation, query synthesis, and validation.**

The ability to generate SQL queries from natural language descriptions bridges the gap between business requirements and database implementation. The open-source **phuryn/pm-skills** repository includes a specialized `sql-queries` skill that automates this translation process, enabling product managers and analysts to produce production-ready SQL without writing manual code. This skill supports multiple database dialects including BigQuery, PostgreSQL, MySQL, and Snowflake.

## Architecture of the SQL Query Generator

According to [`pm-data-analytics/skills/sql-queries/SKILL.md`](https://github.com/phuryn/pm-skills/blob/main/pm-data-analytics/skills/sql-queries/SKILL.md), the skill implements a four-step pipeline:

### Schema Ingestion

When you upload a schema file (SQL DDL, documentation, or diagram description), the parser extracts table names, column definitions, data types, primary/foreign keys, and indexing strategies. This step is optional but strongly recommended for accurate query generation.

### Request Interpretation

The natural language input is clarified to identify target data, SQL dialect requirements, and constraints including filters, aggregates, sorting, and performance limits.

### Query Generation

Using the parsed schema and clarified request, the skill synthesizes an optimized query with helpful comments, alternative approaches, and performance improvements.

### Explanation & Validation

The system returns plain-English explanations of query logic, testing tips, and optional test scripts or sample data for validation.

## Multi-Dialect Support and Syntax Adaptation

The generator automatically adapts syntax to the specified database engine. An internal dialect mapper switches functions, quoting rules, and pagination syntax according to the selected platform—whether PostgreSQL, BigQuery with `ARRAY_AGG`, or MySQL-specific functions.

## Invoking the SQL Generator with write-query

The skill is invoked via the **`write-query`** command defined in [`pm-data-analytics/commands/write-query.md`](https://github.com/phuryn/pm-skills/blob/main/pm-data-analytics/commands/write-query.md). The workflow follows three steps:

1. Upload a schema file (optional but recommended)
2. Issue a concise English prompt describing the desired data retrieval
3. Receive a ready-to-run SQL query with explanations and performance notes

### Example 1: Simple Filter with Schema

```bash
pm-skills write-query
Upload file: database_schema.sql
Prompt: "Generate a query to find users who signed up in the last 30 days and had at least 5 active sessions."

```

**Result:**

```sql
-- PostgreSQL – Users with recent sign‑ups and ≥5 sessions
SELECT u.id,
       u.email,
       COUNT(s.id) AS session_count
FROM   users u
JOIN   sessions s ON s.user_id = u.id
WHERE  u.created_at >= CURRENT_DATE - INTERVAL '30 days'
GROUP  BY u.id, u.email
HAVING COUNT(s.id) >= 5
ORDER  BY session_count DESC;

```

*Explanation*: The query joins `users` and `sessions`, filters the creation date, aggregates session counts, and keeps only rows with ≥5 sessions.  
*Performance note*: Ensure an index on `sessions(user_id, timestamp)`.

### Example 2: BigQuery Revenue Analysis

```bash
pm-skills write-query
Prompt: "Create a BigQuery query to analyze revenue by region and customer tier, including year‑over‑year growth rates."

```

**Result:**

```sql
-- BigQuery – Revenue by region & tier with YoY growth
WITH revenue_by_year AS (
  SELECT
    EXTRACT(YEAR FROM order_date) AS yr,
    region,
    tier,
    SUM(amount) AS rev
  FROM `project.dataset.orders`
  WHERE order_date BETWEEN '2022-01-01' AND '2023-12-31'
  GROUP BY yr, region, tier
)
SELECT
  curr.region,
  curr.tier,
  curr.yr,
  curr.rev,
  SAFE_DIVIDE(curr.rev - prev.rev, prev.rev) AS yoy_growth
FROM revenue_by_year AS curr
LEFT JOIN revenue_by_year AS prev
  ON prev.region = curr.region
 AND prev.tier   = curr.tier
 AND prev.yr = curr.yr - 1
ORDER BY curr.region, curr.tier, curr.yr;

```

*Performance note*: Partition `orders` by `order_date` and cluster by `region, tier`.

### Example 3: Test Script Generation

```bash
pm-skills write-query
Prompt: "Give me a test script that validates the query from Example 1 with sample data."

```

The skill returns a small `INSERT` block plus a `SELECT` that runs the generated query against temporary tables.

## Performance Optimization Features

The generator annotates queries with index recommendations, partitioning hints, and `WHERE` clause ordering suggestions based on schema analysis performed during the ingestion step. These annotations help database administrators optimize execution plans without manual review.

## Summary

- The **sql-queries** skill in `phuryn/pm-skills` translates natural language into SQL via a four-step pipeline documented in [`pm-data-analytics/skills/sql-queries/SKILL.md`](https://github.com/phuryn/pm-skills/blob/main/pm-data-analytics/skills/sql-queries/SKILL.md).
- **Schema ingestion** enables accurate query generation by parsing DDL files and documentation.
- **Multi-dialect support** automatically adapts syntax for PostgreSQL, BigQuery, MySQL, Snowflake, and other engines.
- The **`write-query`** command in [`pm-data-analytics/commands/write-query.md`](https://github.com/phuryn/pm-skills/blob/main/pm-data-analytics/commands/write-query.md) provides the CLI interface for invoking the skill.
- Generated queries include **performance annotations** and **validation scripts** to ensure production readiness.

## Frequently Asked Questions

### What file types does the schema ingestion step support?

The skill accepts SQL DDL files, documentation text, and diagram descriptions. As implemented in [`pm-data-analytics/skills/sql-queries/SKILL.md`](https://github.com/phuryn/pm-skills/blob/main/pm-data-analytics/skills/sql-queries/SKILL.md), the parser extracts table names, column definitions, data types, and key relationships from these uploads to inform query generation.

### Can I generate SQL for multiple database dialects simultaneously?

No, the skill processes one dialect per invocation. However, the internal dialect mapper automatically switches functions, quoting rules, and pagination syntax based on your specified target (BigQuery, PostgreSQL, MySQL, or Snowflake), allowing you to re-run the same natural language prompt against different engines by changing the dialect parameter.

### How does the generator handle complex joins and aggregations?

The **Request Interpretation** phase identifies requirements for filters, aggregates, sorting, and performance limits. For complex scenarios like the BigQuery revenue example, the skill synthesizes Common Table Expressions (CTEs) and self-joins to calculate year-over-year growth, while the **Explanation & Validation** step provides plain-English descriptions of the join logic and aggregation strategy.

### Does the skill provide testing capabilities?

Yes. When requested, the generator produces test scripts that include `INSERT` statements for sample data and validation queries. These scripts allow you to verify the generated SQL against temporary tables before deploying to production databases.