How to Generate SQL Queries from Natural Language Descriptions Using pm-skills
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, 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. The workflow follows three steps:
- Upload a schema file (optional but recommended)
- Issue a concise English prompt describing the desired data retrieval
- Receive a ready-to-run SQL query with explanations and performance notes
Example 1: Simple Filter with Schema
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:
-- 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
pm-skills write-query
Prompt: "Create a BigQuery query to analyze revenue by region and customer tier, including year‑over‑year growth rates."
Result:
-- 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
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-skillstranslates natural language into SQL via a four-step pipeline documented inpm-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-querycommand inpm-data-analytics/commands/write-query.mdprovides 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, 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.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →