Skip to content

SQL Query Constructor

The SQL Query Constructor helps you turn a natural‑language request into a safe, read‑only SQL query that can be run against your database. It checks that the request is only asking for data, translates the request into the language of your database if needed, figures out which columns are relevant, and builds a final SQL statement that returns the requested results.

How it Works

  1. Safety check – The component first verifies that the user’s request is a read‑only operation. If the request tries to insert, update, or delete data, it stops and shows an error.

  2. Language detection – It looks at the user’s request and the content of the database to see if they are in the same language. If they differ, it translates the request into the database’s language.

  3. Column mapping – The component examines the database schema and the user’s request to find which columns (table headers) are relevant. It creates a short “map” of important words to columns.

  4. Query construction – Using the mapped columns and the user’s request, the component asks an LLM (like OpenAI) to draft an SQL query. It then checks the draft for mistakes, ensures it only uses existing columns, and limits the number of rows returned to the value set in Top K.

  5. Result – The final, cleaned‑up SQL query is returned as a plain text message that can be copied or passed to another component for execution.

Inputs

  • Database – Connect the component to the database you want to query.
  • LLM – Choose the language‑model that will interpret the request and build the query.
  • Input request – Type the natural‑language question or instruction you want answered.
  • Top K – Set how many rows the query should return (must be greater than 0).

Outputs

  • Text – A single message containing the final SQL query string. This can be copied, displayed, or fed into a query‑execution component.

Usage Example

  1. Add the component to your workflow.

  2. Configure the inputs:

    • Database: connect to your PostgreSQL database.
    • LLM: select the OpenAI model you prefer.
    • Input request: “Show me the top 5 customers who made the most purchases.”
    • Top K: 5
  3. Run the workflow.

  4. Result – The component outputs a SQL query like:

    SELECT "customer_id", "name", "total_purchases"
    FROM "customers"
    ORDER BY "total_purchases" DESC
    LIMIT 5;

You can then pass this query to a database executor component to get the actual data.

  • Database Connector – Use this to establish a connection to your database.
  • SQL Query Executor – Run the SQL query produced by this component and retrieve the results.
  • LLM – The language model that powers the query construction.

Tips and Best Practices

  • Always set Top K > 0 – The component will raise an error if you enter 0 or a negative number.
  • Keep requests concise – Shorter, clearer questions help the LLM produce more accurate queries.
  • Check the output – Even though the component validates the query, review it before running against production data.
  • Use a read‑only database user – For extra safety, connect with a database account that has only SELECT privileges.

Security Considerations

  • The component blocks any write, update, or delete operations, ensuring only data retrieval is possible.
  • Still, protect your database credentials and restrict access to the component so that only authorized users can generate queries.
  • Review the generated SQL for any unexpected references to sensitive tables before execution.