# How to Generate SQL Queries from Natural Language with PM Skills

> Easily generate SQL queries from natural language using PM Skills. Convert plain English to production-ready SQL for BigQuery PostgreSQL MySQL Snowflake with the /write-query command.

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

---

**PM Skills converts plain-English questions into production-ready SQL statements using the `sql-queries` skill and the `/write-query` slash command, supporting dialects including BigQuery, PostgreSQL, MySQL, and Snowflake.**

The `phuryn/pm-skills` repository provides a dedicated data-analytics plugin that eliminates manual query writing by automatically interpreting user intent and database schemas. By combining natural language processing with schema-aware analysis, this tool generates optimized SQL with explanatory comments, CTEs for readability, and dialect-specific optimizations.

## Architecture Overview

The SQL generation system relies on two core components that work together to process natural language inputs and emit executable code.

### The `sql-queries` Skill

Located at [`pm-data-analytics/skills/sql-queries/SKILL.md`](https://github.com/phuryn/pm-skills/blob/main/pm-data-analytics/skills/sql-queries/SKILL.md), this skill encodes the complete workflow for transforming English descriptions into SQL. It handles schema parsing, dialect selection, and query optimization while ensuring outputs include human-readable commentary and edge-case handling for NULL values, timezones, and duplicate rows.

### 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 slash command serves as the user-facing interface that accepts natural language input, invokes the `sql-queries` skill, and formats the final response with dialect tags, table references, and performance notes.

## How the Pipeline Works

The natural language-to-SQL conversion follows a structured five-step process:

1. **Input Processing** – The assistant receives a description like "Show me daily active users for the last 30 days, broken down by plan tier."

2. **Schema Discovery** – If a schema file (DDL, diagram, or documentation) is uploaded, the skill parses table definitions, primary/foreign keys, and indexes (lines 13-16 of the skill). Without a schema, it requests the database type and falls back to generic SaaS data models.

3. **Intent Resolution** – The skill extracts required metrics, dimensions, filters, time ranges, and grouping parameters while confirming the target SQL dialect (lines 19-21).

4. **Query Generation** – The skill emits an optimized query using CTEs for readability (line 46), inline comments explaining each step (line 25), and explicit handling for edge cases like NULLs and timezones (line 48).

5. **Response Assembly** – `/write-query` formats the output with the dialect identifier, tables used, SQL code block, and explanatory notes covering assumptions and performance recommendations (lines 57-68).

## Practical Code Examples

### Basic Slash-Command Usage

Invoke the command with a plain-English description:

```text
/write-query Show me daily active users for the last 30 days, broken down by plan tier

```

**Generated Output:**

```sql
-- BigQuery dialect
/* Daily active users per plan tier, last 30 days */
WITH recent_events AS (
  SELECT user_id, plan_tier, event_date
  FROM `project.dataset.events`
  WHERE event_date BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY)
    AND CURRENT_DATE()
)
SELECT plan_tier,
       COUNT(DISTINCT user_id) AS daily_active_users
FROM recent_events
GROUP BY plan_tier
ORDER BY daily_active_users DESC;

```

*Dialect:* **BigQuery**  
*Tables used:* `events`  
*Assumptions:* The `events` table contains `user_id`, `plan_tier`, and `event_date` columns.  
*Performance Notes:* The query uses a simple CTE for readability; an index on `event_date` is recommended for large tables.

### Schema-Aware Query Generation

Upload a schema file first to enable precise table and column references:

```text
/upload database_schema.sql
/write-query Generate a query to find users who signed up in the last 30 days and had at least 5 active sessions.

```

**Generated Output:**

```sql
-- PostgreSQL dialect
/* Users with ≥5 sessions in the last 30 days */
WITH signup AS (
  SELECT id, created_at
  FROM users
  WHERE created_at >= CURRENT_DATE - INTERVAL '30 days'
),
session_counts AS (
  SELECT user_id, COUNT(*) AS sess_cnt
  FROM sessions
  WHERE timestamp >= CURRENT_DATE - INTERVAL '30 days'
  GROUP BY user_id
)
SELECT u.id, u.email, sc.sess_cnt
FROM signup u
JOIN session_counts sc ON sc.user_id = u.id
WHERE sc.sess_cnt >= 5;

```

*Dialect:* **PostgreSQL**  
*Tables used:* `users`, `sessions` (extracted automatically from the uploaded schema).

### Multi-Dialect Support

Request the same logic for multiple database systems simultaneously:

```text
/write-query I need the same revenue-by-region query for both BigQuery and MySQL.

```

The assistant returns two distinct code blocks, each annotated with the appropriate dialect syntax and optimizations, allowing direct copy-paste into target environments.

## Key Source Files

| File Path | Description |
|-----------|-------------|
| [`pm-data-analytics/skills/sql-queries/SKILL.md`](https://github.com/phuryn/pm-skills/blob/main/pm-data-analytics/skills/sql-queries/SKILL.md) | Complete specification of the SQL Query Generator skill including architecture, workflow steps, and capabilities. |
| [`pm-data-analytics/commands/write-query.md`](https://github.com/phuryn/pm-skills/blob/main/pm-data-analytics/commands/write-query.md) | Definition of the `/write-query` slash command that orchestrates the skill invocation and response formatting. |
| [`pm-data-analytics/README.md`](https://github.com/phuryn/pm-skills/blob/main/pm-data-analytics/README.md) | Overview of the data-analytics plugin documenting all three available skills (SQL, cohort analysis, and A/B testing). |
| [`pm-data-analytics/.claude-plugin/plugin.json`](https://github.com/phuryn/pm-skills/blob/main/pm-data-analytics/.claude-plugin/plugin.json) | Metadata registering the plugin with Claude Code and Claude Cowork. |

## Summary

- **PM Skills** provides a modular data-analytics plugin that generates SQL from natural language through the `sql-queries` skill and `/write-query` command.
- The workflow ingests optional schema files, resolves user intent, selects appropriate SQL dialects, and emits optimized queries with CTEs and explanatory comments.
- Generated outputs include performance notes, assumptions, and explicit handling for edge cases like NULLs and timezones.
- The system supports BigQuery, PostgreSQL, MySQL, Snowflake, and other dialects, allowing multi-database workflows with a single prompt.

## Frequently Asked Questions

### What SQL dialects does PM Skills support?

According to the `sql-queries` skill implementation, PM Skills supports BigQuery, PostgreSQL, MySQL, Snowflake, and other major SQL dialects. The skill automatically adapts syntax, date functions, and interval notation to match the target database specified in the prompt or schema file.

### Can I generate SQL queries without uploading a schema?

Yes. If no schema file is provided, the skill requests the database type and falls back to a generic SaaS data model. However, uploading a schema file (DDL, diagram, or documentation) enables precise table and column references, resulting in more accurate queries that reflect your actual database structure.

### How does the `/write-query` command handle complex joins?

The command leverages the `sql-queries` skill to analyze uploaded schemas for primary and foreign key relationships (lines 13-16). It generates JOIN clauses automatically based on these relationships, using CTEs to separate logical blocks and adding comments explaining the join conditions and assumptions about table relationships.

### Where can I find the complete skill specification?

The full specification resides 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) within the `phuryn/pm-skills` repository. This document details the complete workflow including schema parsing, intent extraction, dialect selection, and the specific line references (such as lines 19-21 for intent resolution) mentioned in the implementation.