Codecademy Logo

Learn How to Use AI for SQL

AI Prompts

Prompts are what we input to a large language model, and prompt engineering is the art of optimizing prompts to produce effective output.

Guidelines for effective prompting include:

  1. use clear language
  2. understand and communicate the purpose of the prompt
  3. include important context
  4. provide examples to guide the AI’s output toward a specific format

Hallucination in AI

Hallucination is when an AI model generates outputs that are not based on real data but rather are fabricated by the model itself.

For example, an AI model might generate code using syntax that doesn’t actually exist.

Confirmation Bias and AI

As human beings, we are psychologically prone to believing new things that confirm our existing beliefs, known as confirmation bias. If we run code provided by an AI and it produces a result that “makes sense”, we are less likely to catch errors in the AI code.

We need to be very stringent with ourselves when using AI generated code, double-checking everything the AI has done for validity. Note that we can’t use AI to do these checks, as that is using the same flawed system to check itself.

Privacy and Security with AI

The data we feed into an AI model may be used to train future iterations of the model, or stored in some other way, which can result in data leaks where our information becomes part of the publicly available large language model. This poses privacy and security risks if the data is sensitive or confidential.

Conversational Prompting

Instead of writing one large detailed prompt, it is often more effective to interact with an AI system using a conversational approach, adding details as necessary and asking for refinements to build our analytics step by step.

By using a conversational approach, we:

  • Avoid repeating the same information in multiple prompts.
  • Save time crafting overly detailed prompts.
  • Build the habit of reflecting and iterating on the AI output.

Here are some different types of prompts we might write during a conversation:

  • Explain the goal (I’m working on an analytics report about electric vehicles)
  • Provide context (I have three tables with the following names)
  • State your request clearly (Write a query to determine the most efficient vehicle)
  • Review and refine (Can you explain this piece of the query you generated:)
  • Continuing the conversation (That’s great, what other types of statistics should I look into for this report?)

Output Table Prompting

Instead of including every detail in our prompt, we can often get away with telling the AI the final output structure we want, and letting the AI infer how to structure the prompt.

For example:

I have three database tables: models, manufacturers, and fuels. they join using the columns fuel_id and manufacturer_id  
I want a SQL query that produces a table structured as follows:  
| manufacturers.manufacturer | fuels.fuel | number_of_models | 

Notice how

  • We described the three tables and how they join
  • We used table.column notation to communicate which column belongs to which table
  • We used a descriptive name for the third column (number_of_models) in the hope that the AI would calculate this column appropriately

Uses of AI for SQL

AI has many uses in SQL, including:

  • generating queries
  • brainstorming ideas
  • explaining code
  • debugging code

Learn More on Codecademy