How the sql-queries Skill Generates SQL for BigQuery, PostgreSQL, and MySQL from Natural Language

The sql-queries skill transforms natural language requests into optimized, dialect-specific SQL queries for BigQuery, PostgreSQL, and MySQL through a four-phase LLM-driven workflow defined in SKILL.md.

The sql-queries skill is a declarative component in the phuryn/pm-skills repository that enables product managers and analysts to generate production-ready SQL without writing code manually. By leveraging Claude's function-calling capabilities and a structured schema understanding process, the skill produces dialect-aware queries that respect database-specific syntax and performance characteristics.

The Four-Phase Generation Workflow

The skill operates through a deterministic pipeline described 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). Each phase builds upon the previous to ensure accurate, executable SQL output.

Phase 1: Understand Your Database Schema

The skill first parses any provided schema documentation—whether SQL dumps, diagram descriptions, or existing documentation—to build an internal model of your database structure. According to lines 13-17 of SKILL.md, this "Read schema" step extracts table names, column definitions, data types, primary keys, foreign keys, and indexes.

This structured schema model allows the LLM to reference actual table relationships and constraints when constructing joins and filters, eliminating hallucinated column names or invalid table references.

Phase 2: Process Your Request

During the "Process Your Request" step (lines 18-22), the skill analyzes your natural language prompt to identify specific data requirements including filters, aggregations, and join conditions. You must explicitly state the target dialect—BigQuery, PostgreSQL, MySQL, or others—allowing the skill to apply dialect-specific syntax rules.

If critical details are missing, the skill initiates a clarification dialogue to ensure the LLM has complete context before query generation begins.

Phase 3: Generate Optimized Query

The core generation logic resides in the "Generate Optimized Query" step (lines 23-27). Here, Claude produces complete SQL statements that incorporate the schema model and respect the specified dialect's constraints. The skill leverages function-calling to enforce syntactic correctness and includes inline comments, performance hints, and alternative formulations where appropriate.

Unlike template-based generators, this phase creates dynamic SQL without hard-coded patterns, making it adaptable to complex, non-standard schemas.

Phase 4: Explain and Test

The final "Explain and Test" stage (lines 29-33) transforms the raw query into actionable documentation. The skill returns a human-readable explanation of the query logic and suggests validation methods, including sample SELECT statements and test data generation scripts. You can request specific test scripts to verify the query against your actual data.

Dialect-Specific SQL Generation

The sql-queries skill adapts its output syntax to match the unique features and optimization patterns of each supported database engine.

BigQuery Optimization

For BigQuery, the generator employs GoogleSQL-specific constructs including SAFE_CAST, STRUCT, and ARRAY types. It automatically recommends PARTITION BY and CLUSTER BY clauses for large tables to minimize query costs and improve scan efficiency.

PostgreSQL Standards

When targeting PostgreSQL, the skill uses strict ANSI SQL syntax augmented with PostgreSQL-specific ::type casting notation. It suggests CREATE INDEX statements for performance optimization and respects PostgreSQL's handling of INTERVAL arithmetic and window functions.

MySQL Compatibility

For MySQL, the generator prefers LIMIT/OFFSET pagination syntax and carefully handles VARCHAR versus TEXT type nuances. It can emit EXPLAIN hints to help you analyze query execution plans and optimize index usage.

Practical Usage Examples

The following examples demonstrate how natural language prompts translate to dialect-specific SQL across different database engines.

BigQuery: User Activity Analysis

Input prompt:

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

Generated SQL:

-- BigQuery version
SELECT u.id, u.email
FROM `myproject.dataset.users` AS u
JOIN `myproject.dataset.sessions` AS s
  ON u.id = s.user_id
WHERE u.created_at >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
  AND s.session_id IS NOT NULL
GROUP BY u.id, u.email
HAVING COUNT(s.session_id) >= 5;

PostgreSQL: Quarterly Aggregation

Input prompt:

"Give me the average order value per customer for the last quarter, using PostgreSQL syntax"

Generated SQL:

-- PostgreSQL version
SELECT c.customer_id,
       AVG(o.total_amount) AS avg_order_value
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_date >= (CURRENT_DATE - INTERVAL '3 months')
GROUP BY c.customer_id
ORDER BY avg_order_value DESC;

MySQL: Inventory Monitoring

Input prompt:

"Create a MySQL query that lists all products with inventory below 10 units"

Generated SQL:

-- MySQL version
SELECT p.product_id, p.name, i.quantity
FROM products p
JOIN inventory i ON p.product_id = i.product_id
WHERE i.quantity < 10
ORDER BY i.quantity ASC;

Summary

  • The sql-queries skill uses a four-phase workflow (Schema Understanding, Request Processing, Query Generation, Explanation/Testing) defined in pm-data-analytics/skills/sql-queries/SKILL.md.
  • It generates dynamic SQL without hard-coded templates, adapting to BigQuery, PostgreSQL, MySQL, and other dialects through LLM function-calling.
  • Schema awareness ensures queries reference actual table structures, primary keys, and relationships extracted from your documentation.
  • Dialect-specific optimizations include BigQuery partitioning, PostgreSQL type casting, and MySQL execution hints.
  • The skill produces production-ready code with inline comments and testing recommendations, not just raw SQL statements.

Frequently Asked Questions

What databases does the sql-queries skill support?

The skill supports BigQuery, PostgreSQL, MySQL, Snowflake, and SQL Server, with the architecture designed to accommodate additional dialects without code changes. The LLM applies dialect-specific syntax rules based on your explicit specification during the request processing phase.

How does the skill handle different SQL dialects?

The skill relies on Claude's function-calling capabilities to enforce dialect constraints rather than using template-based generation. For BigQuery, it generates GoogleSQL with SAFE_CAST and array structures; for PostgreSQL, it uses ::type casting and ANSI standards; for MySQL, it applies LIMIT/OFFSET pagination and specific type handling.

Is the generated SQL optimized for performance?

Yes. The skill includes performance hints such as PARTITION BY and CLUSTER BY recommendations for BigQuery, CREATE INDEX suggestions for PostgreSQL, and EXPLAIN hints for MySQL. These optimizations appear as inline comments alongside the generated queries.

Can I use the sql-queries skill without providing a schema?

While the skill can generate SQL without a schema, providing schema documentation significantly improves accuracy. The schema parsing step (lines 13-17 of SKILL.md) extracts table relationships and column types, preventing hallucinated table names and ensuring valid join conditions.

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:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →