liveBook · Manning

Self-consistency

As explained in Learn AI Data Engineering

Self-consistency prompting is a prompting technique that elicits multiple solution paths and compares or aggregates them to improve answer reliability.

Self-consistency prompting asks an AI model to solve one problem multiple ways, then compares the outputs to identify the most reliable answer. It is especially useful for SQL, where joins, subqueries, CTEs, and aggregation strategies can express the same metric differently. section 3.4

Use the technique as a practical validation loop:

  • State one SQL problem clearly and request two independent approaches. section 3.4
  • Compare the query logic, not just the syntax; look for different assumptions about joins, grouping, filtering, or counting. section 3.4
  • Run both queries in pgAdmin and confirm that they return the same result. section 3.4
  • Treat divergent answers as a review signal, especially when different AI tools produce different reasoning or SQL preferences. section 3.4

Pagila rental count

Ask: “Using the Pagila dataset, how many customers have rented more than 10 times? Try solving this two different ways.” One approach uses a grouped subquery; another uses a CTE. Both should return 599 when executed and validated in pgAdmin. section 3.4

SELECT COUNT(*) 
FROM (
  SELECT customer_id
  FROM rental
  GROUP BY customer_id
  HAVING COUNT(*) > 10
) AS sub;

Compare the grouped subquery against the CTE alternative to see how equivalent SQL structures can implement the same metric. section 3.4

Lab exercise

Request the top five films by average rental duration using two approaches, then compare whether both queries return the same films even when one uses a CTE and the other does not. section 3.7 section 3.8

How Learn AI Data Engineering covers it

How deep it goes
Demonstrates prompting, query comparison, and pgAdmin validation, but does not develop a formal aggregation method.
Examples
Uses square-root reasoning, Pagila rental counts, and top-five film-duration queries as comparison exercises.
What it assumes
Assumes basic SQL and Pagila familiarity while teaching the comparison-and-validation workflow through concrete prompts.
What you can do after
Diagnose AI-generated SQL with alternate queries, execution checks, and investigation of divergent results.
How it connects
Connects SQL queries with joins, subqueries, common table expressions, aggregation, hallucination, and prompt engineering.

Also explained in

Compare how 8 books explain self-consistency →

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.