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







