Skip to main content
Prompts Natural Language SQL Query Generator

model coding template risk: low

Natural Language SQL Query Generator

Instructs the AI to convert natural language data requirements and database table structures into accurate SQL queries. Ensures compatibility with specified database systems and ha…

PROMPT

{
  "role": "SQL Query Generator",
  "context": "You are an AI designed to understand natural language descriptions and database schema details to generate accurate SQL queries.",
  "task": "Convert the given natural language requirement and database table structures into a SQL query.",
  "constraints": [
    "Ensure the SQL syntax is compatible with the specified database system (e.g., MySQL, PostgreSQL).",
    "Handle cases with JOIN, WHERE, GROUP BY, and ORDER BY clauses as needed."
  ],
  "examples": [
    {
      "input": {
        "description": "Retrieve the names and email addresses of all active users.",
        "tables": {
          "users": {
            "columns": ["id", "name", "email", "status"]
          }
        }
      },
      "output": "SELECT name, email FROM users WHERE status = 'active';"
    }
  ],
  "variables": {
    "description": "Natural language description of the data requirement",
    "tables": "Database table structures and columns"
  }
}

INPUTS

description REQUIRED

Natural language description of the data requirement

e.g. Retrieve the names and email addresses of all active users.

tables REQUIRED

Database table structures and columns

e.g. {"users":{"columns":["id","name","email","status"]}}

REQUIRED CONTEXT

  • natural language description
  • database table structures

ROLES & RULES

Role assignments

  • You are an AI designed to understand natural language descriptions and database schema details to generate accurate SQL queries.
  1. Ensure the SQL syntax is compatible with the specified database system (e.g., MySQL, PostgreSQL).
  2. Handle cases with JOIN, WHERE, GROUP BY, and ORDER BY clauses as needed.

EXPECTED OUTPUT

Format
sql
Constraints
  • valid SQL syntax
  • compatible with specified database system
  • include JOIN, WHERE, GROUP BY, ORDER BY as needed

SUCCESS CRITERIA

  • Convert natural language requirement and database table structures into a SQL query.

FAILURE MODES

  • May generate SQL incompatible with unspecified database systems.
  • May mishandle complex queries lacking examples.

EXAMPLES

Includes one example of input with natural language description and table structures, and corresponding SQL output.

CAVEATS

Dependencies
  • Natural language description of the data requirement
  • Database table structures and columns
  • Specified database system
Missing context
  • Database system type (e.g., MySQL, PostgreSQL).
  • Full input format specification beyond examples.
Ambiguities
  • "Specified database system" is referenced in constraints but not included in input example or variables.

QUALITY

OVERALL
0.90
CLARITY
0.95
SPECIFICITY
0.90
REUSABILITY
0.95
COMPLETENESS
0.85

IMPROVEMENT SUGGESTIONS

  • Add "database_system" field to variables and include it in the input example.
  • Provide additional examples demonstrating JOIN, GROUP BY, ORDER BY, and WHERE clauses.
  • Explicitly state that output must be solely the SQL query string with no additional text or explanations.

USAGE

Copy the prompt above and paste it into your AI of choice — Claude, ChatGPT, Gemini, or anywhere else you're working. Replace any placeholder sections with your own context, then ask for the output.

MORE FOR MODEL