Prompt Claude for Generating SQL Queries
Generating SQL queries is one of the tasks where Claude particularly excels. Whether you are a backend developer, data analyst, or database administrator, formulating complex queries can be time-consuming and error-prone. Claude understands the nuances of different SQL dialects (PostgreSQL, MySQL, SQLite, SQL Server, Oracle) and can transform a natural language description into an optimized and functional query. The AI can handle multiple joins, nested subqueries, advanced aggregation functions, CTEs (Common Table Expressions), and even window functions. By providing Claude with your database schema and a clear description of what you want to obtain, you receive a ready-to-use query along with explanations of the logic used. This significantly speeds up development, reduces bugs related to SQL syntax, and even allows less technical profiles to manipulate complex data with confidence. Claude can also optimize existing queries, detect performance issues, and suggest relevant indexes.
Paste in your AI
Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.
You are an expert in relational databases and SQL. I will provide you with my database schema and a description of what I want to achieve. Generate the corresponding SQL query following these rules:
- Use the following SQL dialect: [POSTGRESQL / MYSQL / SQLITE / SQL_SERVER]
- Write an optimized and readable query with explicit aliases
- Add SQL comments to explain each complex section
- Prefer explicit joins (JOIN ... ON) over implicit joins
- Use CTEs (WITH) if the query is complex to improve readability
Database schema:
[Paste your CREATE TABLE statements or describe your tables with their columns and relationships]
What I want to achieve:
[Describe in natural language the desired result]
Additional constraints:
- Filters to apply: [specify WHERE conditions]
- Desired sorting: [ORDER BY]
- Result limit: [LIMIT if applicable]
After the query, provide:
- A line-by-line explanation of the logic
- Recommended indexes to optimize performance
- Potential pitfalls to watch out for
Personalize this prompt with Léa
Answer 3 questions and Léa tailors the prompt to your situation.
Why this prompt works
This prompt works because it provides Claude with a structured framework including the target SQL dialect, the exact database schema, and precise constraints, eliminating any ambiguity. The request for explanations and index recommendations forces the model to reason deeply about optimization rather than producing a naive query. The numbered step format guides Claude toward an organized and complete output.
Use Cases
Variants
Expected Output
Claude generates a syntactically correct SQL query optimized for the specified dialect, with inline comments explaining the logic of each block. The response also includes a performance analysis with index suggestions and a detailed explanation allowing you to understand and adapt the query as needed.
Frequently Asked Questions
Can Claude handle all SQL dialects?
Claude is proficient in the main SQL dialects: PostgreSQL, MySQL, SQLite, SQL Server, Oracle, and MariaDB. It understands each dialect's specific features, such as PostgreSQL-specific window functions (FILTER, WITHIN GROUP), LIMIT/OFFSET vs. TOP syntax, or differences in date handling. For the best results, always specify the target dialect in your prompt so Claude uses the appropriate syntax and functions.
How can I effectively provide my database schema to Claude?
The most reliable method is to directly paste your CREATE TABLE statements with constraints (PRIMARY KEY, FOREIGN KEY, INDEX). If your schema is too large, focus on the tables involved in your query and mention the key relationships. You can also describe your tables in natural language (e.g., 'users table with id, name, email, creation_date'), but complete DDLs reduce interpretation errors. Remember to include data types, as they influence the functions and conversions Claude will use.
Can Claude optimize an existing SQL query that is too slow?
Yes, this is one of the most powerful use cases. Provide Claude with your current query, the schema of the tables involved, the approximate data volume, and if possible, the EXPLAIN ANALYZE output. Claude will identify bottlenecks (full table scans, inefficient joins, correlated subqueries) and propose an optimized version along with indexes to create. It can transform subqueries into joins, rewrite queries with CTEs for better readability, or suggest alternative approaches like denormalization or materialized views.
Learn more
Check the full skill on Prompt Guide to master this technique from A to Z.
View on Prompt Guide📬 Get new prompts every week
Join our newsletter and never miss a prompt.
Similar Prompts
Generate Mocks and Fixtures for Your Automated Tests
A prompt to automatically generate realistic mocks, stubs and data fixtures adapted to your test framework and use cases.
Automatically Generate Unit Tests with AI
Automatically generate an exhaustive unit test suite covering nominal cases, edge cases, and error cases for any source code.
Create a Python Automation Script
Create a professional Python automation script with CLI configuration, structured logging, error handling, and tests.
Analyze and Optimize Algorithmic Complexity
Analyze the Big O complexity of your algorithms and optimize them with appropriate data structures and more efficient algorithms.