Natural Language Question to SQL

Gives the model a database schema and a question in plain language, and asks for only the SQL query with no code block around it.

  • 🤖 Claude
  • 🗂️ Data

Last verified: write-up checked

Prompt by Anthropic (anthropics/claude-cookbooks) · Original source (opens in a new tab)

Natural Language Question to SQL — prompt preview

The prompt

prompt
Here is the schema for a database:

{schema}

Given this schema, can you output a SQL query to answer the following question? Only output the SQL query and nothing else. Do not wrap it in a code block.

Question: {query}

What it is

Gives the model a database schema and a question in plain language, and asks for only the SQL query with no code block around it. The source notebook runs the generated query against a small test database. The prompt text comes from the Anthropic claude-cookbooks repository (MIT licence, Copyright (c) 2023 Anthropic). Only the code indentation around it was removed. Original author: Anthropic (anthropics/claude-cookbooks).

Who it's for

  • Analysts
  • Developers

يعطي النموذج مخطط قاعدة البيانات وسؤالًا بلغة عادية، ويطلب استعلام SQL فقط من دون أي نص إضافي.

Examples

Question and output from the notebook

Prompt
prompt
What are the names and salaries of employees in the Engineering department?
SELECT name, salary
FROM EMPLOYEES
WHERE department = 'Engineering';

Expected output: The sample question and the query printed in the source, against an EMPLOYEES table.