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
Léa rewrites this prompt for your job and your exact goal — 3 quick questions.
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.
Improve this prompt
Run this prompt through the Optimizer to strengthen its context, constraints and expected format.
Improve this prompt with the OptimizerComments
- LéaAI
Pour de meilleurs résultats, décrivez explicitement les clés étrangères et les cardinalités dans le schéma — cela évite les jointures incorrectes. Si la requête générée est proche mais imparfaite, demandez une révision en précisant l’écart observé.
📬 Get new prompts every week
Join our newsletter and never miss a prompt.
Go further
Similar Prompts
Implement CSP with nonces
Prevent XSS with strict CSP
Mistral Prompt for Creating a Prototype
Mistral, the leading French AI model, stands out as a powerful tool for accelerating prototype creation. Whether you're developing a web application, an API, or a digital product, Mistral can generate functional code, structure your architecture, and produce a testable prototype in minutes. Unlike a traditional approach where prototyping requires several days of development, using Mistral allows you to quickly validate an idea by obtaining a complete functional skeleton. The model particularly excels at generating structured code, proposing coherent architectures, and creating basic but functional user interfaces. By crafting a precise prompt that describes your product vision, key features, and technical constraints, you get a prototype that serves as a solid foundation for iteration. This approach is especially popular with startups, product managers, and independent developers looking to test a market hypothesis without investing weeks of development. Here's how to leverage Mistral to turn your idea into a concrete prototype.
DALL-E Prompt to Generate JavaScript Code
DALL-E, the image generation model developed by OpenAI, is not designed to produce executable JavaScript code. However, it can play a valuable complementary role in a JavaScript developer's workflow. DALL-E excels at creating visuals related to development: user interface mockups, architecture diagrams, data flow schemas, or illustrations to document your code. By crafting precise prompts, you can obtain visual representations of complex JavaScript concepts like closures, the event loop, or design patterns. These visuals then serve as references for implementing your code, creating attractive technical documentation, or designing educational materials. The approach is to use DALL-E as a rapid visual prototyping tool: generate a UI mockup, then translate it into JavaScript components. This method accelerates the design phase and reduces back-and-forth between designers and developers. In this guide, we offer optimized prompts to get the most out of DALL-E in your JavaScript development process, from UI prototyping to visual documentation of your code.
Create a High-Performance Scalable Redis Cache System
A complete prompt to design and implement a Redis cache system with invalidation strategies, error handling and monitoring.