Chain-of-thought prompting asks a language model to work through intermediate reasoning steps before producing an answer. For complex SQL, this exposes assumptions about joins, filters, grouping, and calculations, making generated queries easier to validate and debug without guaranteeing correctness. section 3.3
Use the technique to:
- Request a step-by-step plan before generating SQL. section 3.3
- Inspect proposed joins, aggregations, assumptions, and edge cases before execution. section 3.3
- Ask the model to improve a prompt when a query is long or complicated. section 3.3
Average rentals per active customer
Prompt: “Using the Pagila dataset, calculate the average number of rentals per customer, excluding customers who have never rented anything. Think through the problem step by step before writing the SQL.” The reasoning should identify a customer–rental join, count rentals per customer, exclude customers without rentals, and average those counts. section 3.7 section 3.8
SELECT c.first_name, c.last_name, SUM(p.amount) AS total_spent FROM customer c JOIN payment p ON c.customer_id = p.customer_id GROUP BY c.customer_id, c.first_name, c.last_name ORDER BY total_spent DESC LIMIT 3;”
Review the generated top-customer query as a concrete example of translating stated reasoning into joins, grouping, ordering, and a limit. section 3.3




























