AI Prompts for Claude for SQL

20 of the best prompts for Claude for SQL, step by step across 4 stages. Works with ChatGPT, Claude, and Gemini.

AI Prompts for Claude for SQL

20 of the best prompts for Claude for SQL, step by step across 4 stages. Works with ChatGPT, Claude, and Gemini.

Scroll to explore

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.

Writing SQL Queries

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].

Writing SQL Queries

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.

Writing SQL Queries

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.

Writing SQL Queries

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.

Writing SQL Queries

Convert query

Convert this query to use CTEs instead of subqueries: [PASTE QUERY]. Make it more readable while keeping the same results.

Writing SQL Queries

Query Optimization

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?

Query Optimization

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 Optimization

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 Optimization

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].

Query Optimization

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].

Query Optimization

Schema Design and Data Modeling

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.

Schema Design and Data Modeling

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.

Schema Design and Data Modeling

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?

Schema Design and Data Modeling

Explain normalizing this data

Explain the trade-offs between normalizing this data fully vs. denormalizing for read performance: [PASTE SCHEMA AND DESCRIBE QUERY PATTERNS].

Schema Design and Data Modeling

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.

Schema Design and Data Modeling

Debugging and Data Problems

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?

Debugging and Data Problems

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.

Debugging and Data Problems

JOIN is producing

My JOIN is producing duplicate rows: [PASTE QUERY]. Why is this happening and how do I deduplicate correctly without losing data?

Debugging and Data Problems

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.

Debugging and Data Problems

Review query

Review this query for SQL injection risk: [PASTE QUERY WITH PARAMETERS]. Is it properly parameterized? If not, show the secure version.

Debugging and Data Problems

Frequently asked questions

Which databases does Claude support?+

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.

Can Claude help with ORMs as well as raw SQL?+

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.

Can Claude design a full database for my application?+

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.

How do I share my schema with Claude?+

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.

Can Claude help with database migrations?+

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.