liveBook · Manning

Chapter 3: Using a Coding Companion with SQL

What you will learn in chapter 3 of Learn AI Data Engineering

The chapter introduces using ChatGPT as a coding companion for writing and debugging SQL, with prompting strategies that become more guided as queries grow more complex.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces using ChatGPT as a coding companion for writing and debugging SQL, with prompting strategies that become more guided as queries grow more complex.

Key terms: AI coding companion, ChatGPT, SQL, file uploads, joins, subqueries, Pagila dataset, schema, foreign keys, relationships

3.1 Zero-Shot Prompting

This section introduces zero-shot SQL prompting with Pagila, shows how to validate generated queries, and explains when missing or lost schema context makes them unreliable.

Key terms: zero-shot prompting, Pagila dataset, SQL extracted, pgAdmin, top 5 most rented films, context window, schema drift, schema context, hallucinating

3.2 Few-Shot Prompting

The section teaches how examples and uploaded reference files guide an AI companion to produce SQL in a desired style for a dataset.

Key terms: few-shot prompting, Pagila, Common Table Expression (CTE), aliasing, file uploads, Pagila schema, seed files, extended context, pgAdmin, CTE

3.3 Chain-of-Thought Prompting

This section shows how to ask a coding companion for step-by-step reasoning about SQL, then inspect and verify that reasoning rather than trusting it automatically.

Key terms: chain-of-thought (CoT) prompting, step by step, assumptions, Pagila, payment, customer, customer_id, total amount spent, chain-of-thought cascades, debugging

3.4 Self-Consistency Prompting

The section shows how to compare independently generated SQL solutions, verify their results in a database, and investigate disagreements rather than trusting the first plausible query.

Key terms: self-consistency prompting, joins, subqueries, CTEs, aggregation strategies, Pagila dataset, pgAdmin, subquery, outer count, distinct count

3.5 Tree-of-Thought Prompting

This section shows how Tree-of-Thought prompting helps a coding companion compare SQL approaches and recommend one based on qualities such as readability and maintainability.

Key terms: Tree-of-Thought (ToT) prompting, solution paths, Pagila dataset, direct aggregation, Common Table Expression (CTE), pgAdmin

3.6 Role-Playing, Domain Priming, Prompt Chaining and Beyond

This section introduces additional prompting techniques and explains why effective SQL assistance depends on precise context and validation rather than trust in a plausible-looking query.

Key terms: role-playing, domain priming, prompt chaining, SQL, context, table relationships, schema details, expected outputs, example rows, missing joins

3.7 Lab

This lab practices three SQL prompting techniques by having the reader generate report queries and check alternative solutions.

3.8 Lab Answers

This lab answer section illustrates three ways to prompt for Pagila SQL and emphasizes checking results rather than matching the sample code exactly.

Where this is used later

Concepts explained in this chapter

← Chapter 2: Advantages & Disadvantages of Using a Coding Companion Chapter 4: Using a Coding Companion with Python →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.