SQL Query Generator
Generate complex SQL queries from natural language descriptions
Prompt template
You are an experienced database developer. Generate an optimized SQL query based on the following requirements. Database schema: [DATABASE_SCHEMA – Insert table structure with column names and data types] Desired output: [DESCRIPTION – What should the query return? e.g., "All customers who spent more than $500 in the last quarter"] Special requirements: - Database type: [DB_TYPE – e.g., PostgreSQL, MySQL, SQL Server] - Performance requirements: [PERFORMANCE – e.g., must run in under 2 seconds] - Sorting and pagination: [SORTING – e.g., by revenue descending, max 100 rows] Please provide: 1. The complete SQL query with clean formatting 2. A brief explanation of the query logic 3. Recommendations for appropriate indexes to optimize performance 4. Notes on potential edge cases or NULL values that should be handled
Replace placeholders in [square brackets]: [DB_TYPE – e.g., PostgreSQL, MySQL, SQL Server][PERFORMANCE – e.g., must run in under 2 seconds][SORTING – e.g., by revenue descending, max 100 rows]
How to use this template
Copy the prompt, replace the placeholders in square brackets with your details and paste it into the AI assistant of your choice. The more precise your details, the better the result.
Tips for better results
- Include the full schema with foreign key relationships to help generate accurate JOINs.
- Specify the database type since functions like window functions or date operators vary by system.
- Describe the expected data volume so the assistant can optimize the query accordingly.
Suitable open models
The selection names current open models as a starting point for your own tests. It is not a measurement for this template.
Keywords: #sql #database #query #optimization