Skip to main content
Data Prompts

Write a MySQL Query

Generate MySQL queries from natural language descriptions, including JOINs and aggregations.

beginnerWorks with any modelData
Prompt
Write a MySQL query based on the following description.

**Tables and schema:**
[TABLES]
(Describe each table: name, key columns, data types, and how tables relate to each other — e.g., "orders.customer_id references customers.id")

**What I need:**
[WHAT_YOU_NEED]
(Describe the result in plain English — e.g., "total sales amount per customer for the last 30 days, sorted highest to lowest")

**Performance note (optional):**
[PERFORMANCE_NOTE]
(e.g., "the orders table has 10M+ rows" or "queries run on a read replica" — leave blank if not applicable)

Please provide:

1. **MySQL query** — complete, runnable SQL with inline comments explaining each major clause.

2. **Explanation of JOIN logic** — if the query involves JOINs, explain in plain English which tables are joined, on which keys, and why that join type (INNER, LEFT, etc.) was chosen.

3. **Suggested indexes** — if the query is likely to be slow without proper indexing, list the columns that should be indexed and why. Format as ready-to-run CREATE INDEX statements.

4. **Edge cases to watch for** — note any assumptions made (e.g., NULL handling, date timezone, duplicate rows) that might cause unexpected results on real data.

How to Use

Describe your table structure in plain English in [TABLES] — you do not need to paste a full CREATE TABLE statement, though you can. Be specific about column names and data types for the columns involved in your query. Describe the output you want in [WHAT_YOU_NEED] as if explaining it to a colleague. The generated query will include comments so you can understand and modify it.

Variables

VariableDescription
[TABLES]Table names, relevant columns, data types, and foreign key relationships
[WHAT_YOU_NEED]Plain-English description of the data you want to retrieve
[PERFORMANCE_NOTE]Optional: table size, replication setup, or other constraints that affect query design

Tips

  • The more specific your schema description, the more accurate the query — include column names exactly as they appear in your database.
  • If you get a query that is logically correct but slow, re-run the prompt with your EXPLAIN output pasted in and ask for optimization.