3 Using a Coding Companion with SQL
The chapter shows how an AI coding companion, especially ChatGPT, can help write, debug, and refine SQL when used with the right prompting strategy. Since SQL is central to data engineering, the key skill is learning how to guide the model with enough context, then validate the generated query in a database tool rather than trusting the response blindly. The Pagila dataset is used throughout because its schema is already familiar to the model, making it a convenient setting for testing different prompting methods.
It begins with the simplest approach, zero-shot prompting, where a direct request can often produce a useful query with little setup. From there, the chapter builds toward few-shot prompting, where examples help the model imitate a preferred structure and formatting style, especially for recurring report patterns. It also emphasizes the value of uploaded schema files or other reference material, since real-world datasets are usually private and unfamiliar to the model.
More advanced techniques such as chain-of-thought, self-consistency, and tree-of-thought are presented as ways to improve clarity, reliability, and debugging. Step-by-step reasoning can expose faulty assumptions, multiple solutions can reveal inconsistencies, and comparing alternative query paths can highlight the clearest or most maintainable option. The overall message is that AI is most effective for SQL when prompts are precise, context is supplied carefully, and outputs are treated as drafts that must be tested and verified.
This diagram presents a visual summary of the prompting strategies explored in Chapter 3. It shows how each technique builds on the basic prompt–response pattern by introducing added context, reasoning steps, or multiple outputs. Starting with zero-shot prompting, each method increases in complexity and interpretability—from few-shot and chain-of-thought to self-consistency and tree-of-thought—helping you guide large language models with greater control and clarity.
Result of the Zero-Shot Prompt response for the Pagila data set.
Demonstrating the file upload capability of ChatGPT. Uploading files works much like any web-based tool: you click to add a file, and it becomes part of the active conversation. Once uploaded, ChatGPT can read and reference the contents of your schema or data files to generate more accurate and context-aware SQL.
Result of the Few-Shot Prompt response.
Result of the Chain-of-Thought Prompt response.
Result of the Tree-of-Thought Prompt response.
Lab Answers
Keep in mind that your SQL code may not match these answers exactly. The AI may use different alias names, formatting, or logic structures, but the results should be consistent when run against the Pagila dataset. The key is that the output is correct, not that the code looks identical.
1. Chain-of-Thought Prompting
2. Few-Shot Prompting
3. Self-Consistency Prompting
Learn AI Data Engineering ebook for free