Natural Language to SQL
Natural Language to SQL turns a question written in everyday words, such as “how many orders did we have in March in Madrid?”, into an SQL query ready for your database. The AI model knows the structure of your tables because the component shows it, so it writes the query with the real table and column names.
The component only writes the query; it doesn’t run it. Use it when you want people without SQL knowledge to request data, or to prepare queries that someone on the team reviews afterwards.
When to use it
- A sales manager asks by chat “which five customers bought the most this quarter?” and gets the query to run.
- Prepare queries for a weekly report from questions written by the business team.
- Help someone new to SQL see how the query that answers their question is written.
Configuration
| Field | What it does |
|---|---|
| Input (required) | The question in natural language. You can type it or connect it, for example, to a Chat Input. |
| Top K | The maximum number of results the query should return when the question doesn’t say otherwise. Default: 5. |
| Prompt | Optional. Leave it empty to use the component’s own instructions, which already include the structure of your tables. If you write your own, it must contain {question} (the question) and can use {table_info} (the structure of your tables), {top_k} and {dialect}. |
| Connection | What to connect |
|---|---|
| Model (required) | An AI model component, for example OpenAI. |
| SQLDatabase (required) | The database the query is written for: the SQLDatabase output of the SQLDatabase component. |
Output
Text is the SQL query, as text. You can show it in a Chat Output so someone can review it, or pass it to a component that runs queries on your database.
Example
Distribuciones Encina wants its sales team to request data without knowing SQL:
- Add an SQLDatabase connected to the orders database and connect its output to SQLDatabase.
- Connect a Chat Input to Input and a model to Model.
- Connect Text to a Chat Output.
When someone writes “which three products sold the most in April?”, the chat returns the SQL query with the real tables and columns of the orders database.
Good practices
- Review the queries before running them on a production database.
- Set up SQLDatabase with a database user that only has read permissions.
- Write specific questions, with clear dates and criteria: the query will be more precise.