3 Using a Coding Companion with SQL

 

In this chapter, we focus on using an AI coding companion—specifically ChatGPT—to write and debug SQL. SQL is the foundational language of data engineering, and it pairs surprisingly well with modern LLMs when prompted effectively. As we progress through this chapter, you’ll learn how to adapt your prompts based on the complexity of your task, how to leverage new LLM features like file uploads, and how to guide the model through intricate query structures like joins, subqueries, and window functions.

We’ll continue using the Pagila dataset, a popular open-source database based on a DVD rental business. Since this dataset is so widely used, ChatGPT already knows a great deal about its schema, foreign keys, and relationships. That means a prompt that starts with “Using the Pagila dataset...” often gives us excellent results with minimal setup.

The power of a coding companion really shines when we go beyond zero-shot prompting. You'll learn how few-shot prompting, chain-of-thought, and even prompt chaining can turn your companion from a helpful assistant into a full-on query whisperer. We'll explore each prompting style in depth with general examples, Pagila-specific applications, and "Try it now" exercises to help you build real fluency.

3.1 Zero-Shot Prompting

3.2 Few-Shot Prompting

3.3 Chain-of-Thought Prompting

3.4 Self-Consistency Prompting

3.5 Tree-of-Thought Prompting

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

3.7 Lab

3.8 Lab Answers