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

> Learn to generate SQL queries from natural language using pm-skills. This tool converts plain English into optimized SQL for PostgreSQL BigQuery MySQL and Snowflake.

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

---

**The pm-skills repository provides a SQL Query Generator skill that converts plain-English requirements into optimized SQL statements for PostgreSQL, BigQuery, MySQL, and Snowflake through a four-step pipeline.**

The ability to generate SQL queries from natural language descriptions eliminates the technical barrier between business requirements and database implementation. The `phuryn/pm-skills` open-source project implements this capability through a dedicated `sql-queries` skill defined 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), enabling product managers to produce dialect-specific SQL without manual coding.

## Understanding the SQL Query Generator Architecture

According to the skill specification 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 generator implements a deterministic four-step pipeline to transform English prompts into executable database commands.

**Schema Ingestion** parses uploaded schema files—including SQL DDL, documentation, or diagram descriptions—to extract table names, column definitions, data types, primary/foreign keys, and indexing strategies. This step enables the generator to understand database relationships without hard-coded assumptions.

**Request Interpretation** clarifies the natural-language input to identify the target data, desired SQL dialect, and constraints such as filters, aggregates, sorting, or performance limits.

**Query Generation** synthesizes an optimized SQL statement using the parsed schema and clarified request. The skill inserts inline comments explaining the logic and suggests alternative approaches or performance improvements.

**Explanation & Validation** returns a plain-English explanation of the query logic alongside tips for testing results and optional test scripts with sample data.

## Invoking the Natural Language to SQL Pipeline

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). This command wires the SQL generation capability into the `pm-skills` CLI interface.

The standard workflow follows three steps:

1. Upload a schema file (optional but strongly recommended for accurate table/column references).
2. Issue a concise English prompt describing the desired data retrieval.
3. Receive a ready-to-run SQL query with dialect-specific syntax, performance notes, and validation guidance.

```text
> 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."

```

## Multi-Dialect SQL Generation Capabilities

The generator adapts syntax automatically to the specified database engine through an internal dialect mapper. This component switches functions, quoting rules, and pagination syntax according to the selected platform.

For example, the mapper translates standard SQL functions to BigQuery-specific alternatives like `ARRAY_AGG` or adjusts date arithmetic between PostgreSQL (`INTERVAL '30 days'`) and MySQL (`DATE_SUB(NOW(), INTERVAL 30 DAY)`). This ensures generated queries execute correctly without manual syntax correction.

## Practical Code Examples

The following examples demonstrate the skill's output for different dialects and analytical requirements.

### PostgreSQL User Session Analysis

When provided with a user and session schema, the generator produces optimized join logic with appropriate filtering and aggregation.

**Input Prompt:**

```text
"Generate a query to find users who signed up in the last 30 days and had at least 5 active sessions."

```

**Generated SQL:**

```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;

```

**Performance Note:** The skill recommends ensuring an index on `sessions(user_id, timestamp)` to optimize the join and filter operations.

### BigQuery Revenue Analysis with Year-over-Year Growth

For BigQuery datasets, the generator uses Common Table Expressions (CTEs) and dialect-specific functions like `SAFE_DIVIDE`.

**Input Prompt:**

```text
"Create a BigQuery query to analyze revenue by region and customer tier, including year‑over‑year growth rates."

```

**Generated SQL:**

```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:** The skill suggests partitioning the `orders` table by `order_date` and clustering by `region, tier` to minimize query costs.

### Automated Test Script Generation

The skill can generate validation scripts to verify query logic against temporary sample data.

**Input Prompt:**

```text
"Give me a test script that validates the query from Example 1 with sample data."

```

**Result:** The generator produces a complete test suite including `INSERT` statements to populate temporary tables and a `SELECT` statement that executes the generated query against that sample data, enabling immediate validation of the logic.

## Key Source Files and Implementation Details

The natural language to SQL conversion is implemented across the following repository locations:

- **[`pm-data-analytics/skills/sql-queries/SKILL.md`](https://github.com/phuryn/pm-skills/blob/main/pm-data-analytics/skills/sql-queries/SKILL.md)** — Contains the complete skill specification including the four-step pipeline, capabilities, and usage guidelines.
- **[`pm-data-analytics/commands/write-query.md`](https://github.com/phuryn/pm-skills/blob/main/pm-data-analytics/commands/write-query.md)** — Defines the CLI command entry point that invokes the `sql-queries` skill via `pm-skills write-query`.
- **[`README.md`](https://github.com/phuryn/pm-skills/blob/main/README.md)** (line ≈ 316) — Lists the `sql-queries` skill among the repository's available capabilities in the high-level skill inventory.
- **[`pm-data-analytics/README.md`](https://github.com/phuryn/pm-skills/blob/main/pm-data-analytics/README.md)** — Provides an overview of the data-analytics module and links to all analytic skills within that domain.

## Summary

- The **SQL Query Generator** skill in `phuryn/pm-skills` transforms English descriptions into executable SQL through schema ingestion, request interpretation, query synthesis, and validation.
- 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) serves as the primary interface for invoking the generator.
- **Multi-dialect support** includes PostgreSQL, BigQuery, MySQL, and Snowflake, with automatic syntax adaptation via an internal dialect mapper.
- **Schema upload** is optional but significantly improves accuracy by providing table structures, relationships, and column types.
- Generated outputs include **performance optimizations** such as index recommendations, partitioning hints, and `WHERE` clause ordering suggestions.

## Frequently Asked Questions

### Which SQL dialects does the pm-skills SQL generator support?

The skill supports PostgreSQL, BigQuery, MySQL, and Snowflake. The internal dialect mapper automatically adjusts function syntax, quoting rules, and pagination patterns to match the target platform specified in the natural language request.

### Is uploading a schema file mandatory for query generation?

No, schema upload is optional but strongly recommended. Without schema context, the generator infers table structures from the prompt alone. Uploading SQL DDL or documentation files enables the skill to reference actual column names, data types, and relationships, producing more accurate queries.

### How does the skill optimize query performance?

During the **Query Generation** step, the skill analyzes the ingested schema to annotate outputs with index recommendations, partitioning hints, and `WHERE` clause ordering suggestions. It also structures joins and aggregations to leverage existing indexes identified during schema parsing.

### Can the generator produce validation test scripts?

Yes. When requested, the skill enters the **Explanation & Validation** step to generate test scripts containing `INSERT` statements for temporary sample data and corresponding `SELECT` statements to validate the query logic against that data.