⌨️ Codingintermediatesqldatabasequerypostgresqldata

Write a SQL Query from Plain English

Describe what data you need in plain English and get the correct SQL query with an explanation.

The Prompt

prompt.txt
Write a SQL query based on the following description. Include:
1. The complete SQL query
2. An explanation of each clause (FROM, JOIN, WHERE, GROUP BY, etc.)
3. Any indexes you'd recommend for performance
4. Alternative approaches if there's a simpler way to write it

Database type: [POSTGRESQL/MYSQL/SQLITE/MSSQL]

Table schema:
```sql
[PASTE YOUR TABLE DEFINITIONS OR DESCRIBE YOUR TABLES]
```

What I need:
[DESCRIBE IN PLAIN ENGLISH — e.g., Find all users who made more than 3 purchases in the last 30 days, ordered by total spend descending, with their email and total amount spent]

Example Output

Generated a query using a CTE to pre-aggregate purchases per user in the last 30 days, then joining back to the users table with a HAVING clause. Recommended a composite index on (user_id, created_at) for performance on large tables.

FAQ

Which AI model is best for Write a SQL Query from Plain English?

GPT-4o or Claude Sonnet 4. Gemini 2.5 Pro is excellent for complex multi-table queries.

How do I use the Write a SQL Query from Plain English prompt?

Copy the prompt, replace the [BRACKETED] placeholders with your specific information, and paste into your preferred AI assistant (ChatGPT, Claude, Gemini, etc.). Generated a query using a CTE to pre-aggregate purchases per user in the last 30 days, then joining back to the users table with a HAVING clause. Recommended a composite index on (user_id, created_at) for performance on large tables.