Home
cd ../playbooks
Data & ReportingIntermediate

Natural Language SQL Writer

Write optimized SQL queries from plain English across multiple database dialects

10 minutes
By AnthropicSource
#sql#queries#natural-language#database

You know exactly what data you need but the SQL to get it involves three JOINs, a subquery, and a CASE statement you can never remember. You spend 20 minutes writing a query that returns wrong results, then another 20 debugging it.

Who it's for: business analysts who understand data but struggle with SQL syntax, product managers pulling their own metrics from the data warehouse, data analysts working across PostgreSQL and BigQuery and Snowflake, non-technical team members who need data but depend on engineering, marketers querying campaign databases for custom reports

Example

"Show me monthly revenue by product category for customers who signed up in Q1" → Optimized SQL with proper JOINs, date filtering, GROUP BY with window functions, tested against PostgreSQL and BigQuery dialects, with comments explaining each section

CLAUDE.md Template

New here? 3-minute setup guide → | Already set up? Copy the template below.

Or install from your terminal:
npx ccplaybooks add natural-language-sql-writer
# Natural Language SQL Writer

## Your Role
You are a SQL query assistant. You write optimized SQL queries from plain English descriptions, following best practices for performance, readability, and correctness across multiple database dialects.

## Workflow

### 1. Understand the Request

Parse the description to identify:
- **Output columns**: What fields should the result include?
- **Filters**: What conditions limit the data (time ranges, segments, statuses)?

Get new playbooks like this one

One email a week with new Claude Code workflows. Free, like everything here.

No spam. Unsubscribe anytime.

README.md

What This Does

Translates plain English descriptions into optimized SQL queries for your specific database dialect. The assistant writes readable, performant queries using CTEs, proper JOIN types, and dialect-specific syntax. It supports PostgreSQL, Snowflake, BigQuery, Redshift, Databricks, MySQL, SQL Server, DuckDB, and SQLite.


Quick Start

Step 1: Download the Template

Click Download above to get the CLAUDE.md file.

Step 2: Set Up Your Project

Create a project folder and place the template inside:

mkdir -p ~/Projects/sql-queries
mv ~/Downloads/CLAUDE.md ~/Projects/sql-queries/

Optionally add schema documentation or sample data files to the folder.

Step 3: Start Working

cd ~/Projects/sql-queries
claude

Say: "Write a query to count orders by status for the last 30 days"


Supported Dialects

  • PostgreSQL (including Aurora, RDS, Supabase, Neon)
  • Snowflake
  • BigQuery (Google Cloud)
  • Redshift (Amazon)
  • Databricks SQL
  • MySQL (including Aurora MySQL, PlanetScale)
  • SQL Server (Microsoft)
  • DuckDB
  • SQLite

Mention your dialect upfront to get the right syntax immediately.


Query Best Practices Applied

Every generated query follows these principles:

  • CTEs for readability -- One CTE per logical step, with descriptive names like daily_signups or revenue_by_product
  • Performance-first -- Filters pushed early, partition keys used, SELECT * avoided, appropriate JOIN types
  • Commented logic -- Non-obvious reasoning explained inline
  • Clean formatting -- Consistent indentation, meaningful table aliases, one clause per line

Tips

  • Mention your dialect upfront -- "We use Snowflake" gets you correct syntax immediately without a follow-up question.
  • Include table names if known -- "Query the orders table joined with customers" saves a round trip.
  • Specify if parameterized -- For recurring queries, mention "make it parameterized for date range" to get reusable templates.
  • Ask for performance notes -- The assistant can highlight partition usage, expected cost, and potential bottlenecks.

Example Prompts

"Count of orders by status for the last 30 days in PostgreSQL"
"Cohort retention analysis -- group users by signup month, show activity at 1, 3, 6, and 12 months"
"Top 100 users by event count in the last 7 days from a 500M row events table partitioned by date"
"Write a Snowflake query to calculate monthly recurring revenue by product line"
"Convert this BigQuery query to PostgreSQL syntax"

$Related Playbooks

Browse all Data & Reporting playbooks →