liveBook · Manning

Chain-of-thought prompting

As explained in Learn AI Data Engineering

Chain-of-thought prompting is a prompting technique that asks a language model to produce or follow an explicit sequence of intermediate reasoning steps before an answer.

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

How Learn AI Data Engineering covers it

How deep it goes
Defines step-by-step prompting and demonstrates it with SQL and arithmetic, while noting that exposed reasoning does not guarantee correctness.
Examples
Uses arithmetic division, Pagila customer spending, average rentals, and queries involving joins, grouping, ordering, and limits.
What it assumes
Assumes familiarity with basic SQL and relational datasets while reinforcing joins, grouping, counting, and averaging through guided reasoning.
What you can do after
Diagnose/debug generated SQL by inspecting its intermediate joins, calculations, assumptions, and edge-case handling before execution.
How it connects
Connects with zero-shot prompting, few-shot prompting, prompt chaining, self-consistency prompting, joins, and aggregations.

Also explained in

Compare how 29 books explain chain-of-thought prompting →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI Data Engineering. The section links point to the parts of the book it draws on.