20 of the best prompts for Claude for SQL, step by step across 4 stages. Works with ChatGPT, Claude, and Gemini.
20 of the best prompts for Claude for SQL, step by step across 4 stages. Works with ChatGPT, Claude, and Gemini.
Write complex SQL queries, optimize slow ones, and understand your database better with Claude. This guide walks you through every stage of Claude for SQL, from Writing SQL Queries all the way through Debugging and Data Problems, with a curated, copy-ready prompt at each step. Each stage targets a specific phase of the process so you always know exactly what to ask and what output to expect. Works with ChatGPT, Claude, and Gemini and any other major AI tool.
Claude writes accurate, readable SQL for any database system from plain-language descriptions.
Write SQL query
Write a SQL query to [DESCRIBE WHAT YOU NEED]. My schema is: [PASTE RELEVANT TABLE DEFINITIONS]. Database: [POSTGRESQL / MYSQL / SQLITE / BIGQUERY / OTHER].
Join tables:
I need to join these tables: [LIST TABLES WITH KEYS]. Return [DESCRIBE THE DATA NEEDED]. Filter by [CONDITIONS]. Order by [FIELD]. Write the query with clear aliases and formatting.
Write query
Write a query that [COMPLEX TASK, E.G., FINDS DUPLICATES / CALCULATES RUNNING TOTALS / RANKS RESULTS / PIVOTS DATA]. Schema: [PASTE SCHEMA]. Explain the approach before writing the SQL.
Result should show
I need to aggregate [DATA] from [TABLES] grouped by [DIMENSIONS] with [CONDITIONS]. The result should show [DESCRIBE OUTPUT]. Write the query and explain any window functions or CTEs used.
Convert query
Convert this query to use CTEs instead of subqueries: [PASTE QUERY]. Make it more readable while keeping the same results.
Claude analyzes slow queries and suggests targeted optimizations.
Query is running
This query is running slowly: [PASTE QUERY]. Schema: [PASTE SCHEMA WITH ANY KNOWN INDEXES]. What is likely causing the slowness, and what are the highest-impact optimizations?
Explain execution plan
Explain the execution plan for this query: [PASTE QUERY AND EXPLAIN OUTPUT]. What is the most expensive operation and how do I speed it up?
Query does
This query does a full table scan. How do I refactor it or add indexes to avoid it? [PASTE QUERY AND SCHEMA].
Query runs
I have this query that runs in [TIME] on a table with [ROW COUNT] rows. At [TARGET SCALE], it will be too slow. What changes will make it scale? [PASTE QUERY AND SCHEMA].
Compare Compare these two
Compare these two queries that return the same result and tell me which is more efficient and why: [QUERY A] vs [QUERY B]. Schema: [PASTE SCHEMA].
Claude helps design schemas, normalize data, and model relationships correctly.
Design database schema
I need to design a database schema for [DESCRIBE APPLICATION]. Here are the main entities and relationships: [DESCRIBE]. Design a normalized schema with appropriate primary keys, foreign keys, and indexes.
Review schema
Review this schema for issues: [PASTE SCHEMA]. Check for: normalization problems, missing indexes, data type choices, naming consistency, and anything that will cause problems at scale.
Best way
I need to add [FEATURE] to my existing schema: [PASTE SCHEMA]. What is the best way to model this without breaking existing queries or requiring a full migration?
Explain normalizing this data
Explain the trade-offs between normalizing this data fully vs. denormalizing for read performance: [PASTE SCHEMA AND DESCRIBE QUERY PATTERNS].
Design indexes
Design indexes for this table to support these query patterns: [LIST QUERIES]. Schema: [PASTE TABLE DEFINITION]. Explain the trade-offs of each index.
Claude diagnoses SQL errors, wrong results, and data integrity issues.
Getting SQL error:
I am getting this SQL error: [PASTE ERROR]. Here is the query: [PASTE QUERY]. What is causing it and how do I fix it?
Query is returning
This query is returning wrong results: [PASTE QUERY]. Expected: [DESCRIBE]. Actual: [DESCRIBE]. Walk through the logic and identify where the error is.
JOIN is producing
My JOIN is producing duplicate rows: [PASTE QUERY]. Why is this happening and how do I deduplicate correctly without losing data?
Data integrity issues
I have data integrity issues in this table: [DESCRIBE PROBLEM, E.G., ORPHANED FOREIGN KEYS, NULLS WHERE NOT EXPECTED, DUPLICATE RECORDS]. Write queries to identify and fix the bad data.
Review query
Review this query for SQL injection risk: [PASTE QUERY WITH PARAMETERS]. Is it properly parameterized? If not, show the secure version.
Claude writes SQL for PostgreSQL, MySQL, SQLite, SQL Server, Oracle, BigQuery, Snowflake, Redshift, and other ANSI SQL systems. Mention your database, syntax and function names vary across platforms.
Yes. Claude works with Prisma, SQLAlchemy, Django ORM, ActiveRecord, TypeORM, and others. It can translate between raw SQL and ORM syntax, or explain what ORM-generated SQL is doing.
Yes. Describe your application, its entities, relationships, and expected query patterns. Claude designs a schema, explains the normalization choices, and suggests indexes. Review it with a DBA before using in production.
Paste the CREATE TABLE statements. If the schema is very large, paste only the relevant tables. Include any existing indexes, they are critical for optimization advice.
Yes. Describe the current schema and what needs to change. Claude writes the migration SQL and flags operations that will lock tables, require downtime, or risk data loss at scale.
AI Prompts for Claude for Python
Write cleaner Python faster, debug stubborn errors, and level up your Python skills with Claude as your coding partner..
See promptsAI Prompts for Claude for Debugging
Diagnose and fix bugs faster by using Claude as a methodical debugging partner that explains root causes..
See promptsAI Prompts for Claude for Code Review
Get thorough, senior-level code reviews that catch real issues, enforce standards, and improve code quality..
See prompts