Generative AI for Data Workflows

Prompt Engineering, RAG, and SQL Generation

**Generative AI** is transforming how analysts interact with data. Instead of writing SQL manually, you can **prompt** an LLM to generate it. Instead of searching through documentation, **RAG (Retrieval-Augmented Generation)** feeds relevant context to the model automatically. For **Walmart**, this means a supply chain manager can ask "What's the average transit time for perishables in Q4?" and get instant SQL + results.

In this chapter, we explore **Prompt Engineering** best practices, the **RAG** architecture, and how to build a **Text-to-SQL** pipeline that turns natural language into database queries.

Prompt Engineering: The Art of Asking

The quality of an LLM's output depends almost entirely on the quality of the **prompt**. A vague prompt produces vague results. **Prompt Engineering** is the practice of crafting precise, structured instructions. For **Netflix**, this means the difference between getting "Recommend some shows" vs "Recommend 5 sci-fi shows released after 2020 with rating > 8.5, focusing on strong female leads."

PythonRuns entirely in your browser — nothing is sent to a server.

RAG: Retrieval-Augmented Generation

**RAG** solves the LLM hallucination problem by grounding the model's responses in real data. Instead of relying on the model's training data (which can be outdated), RAG retrieves relevant documents from your database and feeds them as context to the prompt. For **Walmart**, this means the LLM answers supply chain questions using actual inventory data, not guesses.

PythonRuns entirely in your browser — nothing is sent to a server.

Text-to-SQL: Natural Language to Database Queries

**Text-to-SQL** allows non-technical users to query databases in plain English. For **Visa**, a compliance officer can ask "Show me all transactions over $10,000 from foreign merchants in the last 30 days" and get a ready-to-run SQL query. This democratizes data access across the organization.

PythonRuns entirely in your browser — nothing is sent to a server.

Practice Questions

Question 1

What problem does RAG solve that standard LLMs cannot?

  • It makes the model faster
  • It grounds the model's responses in real, retrieved data, preventing hallucination and ensuring answers are based on current facts
  • It replaces the need for a database
  • It only works with GPT-4

Question 2

Why is Prompt Engineering considered a critical skill for data analysts?

  • Because it reduces the cost of electricity
  • Because the precision and structure of the prompt directly determines the quality and relevance of the LLM's output
  • Because it replaces Python
  • Because it only works in English