Skip to content

Natural Language to SQL

Natural Language to SQL is a component that lets you ask a question in plain English and get back a SQL query that can be run against your database. It’s useful when you want to quickly explore data without writing SQL yourself.

How it Works

When you type a question into the Input field, the component sends that question to a language model (LLM) that has been trained to understand natural language. The LLM uses the database schema you provide through the SQLDatabase input to create a SQL query that answers the question. The Top K setting controls how many rows the query will return. If you supply a custom Prompt, the component will use that prompt template instead of the default one, as long as it contains the placeholder {question}.

Inputs

  • SQLDatabase: Connect your database so the component knows the tables and columns it can query.
  • Model: Choose the language model that will generate the SQL.
  • Input: Write the natural‑language question you want answered.
  • Prompt: (Optional) Provide a custom prompt template that includes {question}. This lets you tweak how the model formulates the query.
  • Top K: Set the maximum number of rows the generated query should return. The default is 5.

Outputs

  • Text: The component returns the generated SQL query as a text message. You can feed this output into another component that executes the query or display it to the user.

Usage Example

  1. Drag the Natural Language to SQL component onto the canvas.
  2. Connect a Database Connector component to the SQLDatabase input.
  3. Connect a LLM component (e.g., OpenAI GPT‑4) to the Model input.
  4. In the Input field type:
    Show me the top 10 customers who spent the most last month.
  5. Leave Prompt empty to use the default prompt.
  6. Click Run.
    The component will output a SQL query like:
    SELECT * FROM customers WHERE purchase_date >= ‘2023-08-01’ AND purchase_date <= ‘2023-08-31’ ORDER BY total_spent DESC LIMIT 10;
  • Database Connector – Connects to your database and provides the SQLDatabase input.
  • SQL Executor – Takes a SQL query and runs it against the database, returning the results.
  • LLM – Supplies the language model used to generate the query.

Tips and Best Practices

  • Keep the Top K value reasonable to avoid returning huge result sets.
  • Review the generated SQL before executing it, especially if the query will modify data.
  • If you need more control over the query structure, provide a custom Prompt that guides the model.

Security Considerations

The component generates SQL based on user input, so it can produce queries that read or modify data. Always validate the output before execution and restrict the database user’s permissions to only the tables and actions that are necessary for your workflow.