# Query Database

{% embed url="<https://www.youtube.com/embed/eFb4O4LDg1s>" %}

The Query Database block allows your AI Agent to connect to and query an external SQL database during runtime. It supports PostgreSQL, MySQL, and Microsoft SQL Server, making it ideal for workflows that require live data access — such as retrieving customer records, product inventory, or historical logs.

***

## Configurations

### Account

Connect the database account your agent will use to run queries.

You can choose:

* **Static Account:** A fixed database connection defined at build time.
* **Dynamic (Ask at Runtime):** Prompts the user to input database credentials during execution.

To connect, select your database type (PostgreSQL, MySQL, etc.) and enter:

| Info     | Example               |
| -------- | --------------------- |
| Host     | `db.abcd.supabase.co` |
| Port     | `5432`                |
| Database | `postgres`            |
| Username | `postgres`            |
| Password | *YOUR SECRET KEY*     |

***

### Query

Enter the SQL query to run against the external database. You can use dynamic variables with `{{brackets}}` to inject content from earlier in the workflow.

**Example (Static Query):**

```sql
SELECT * FROM users WHERE last_name = 'Williams';
```

**Example (Dynamic Query using variable):**

```sql
SELECT * FROM users WHERE last_name = '{{last_name}}';
```

{% hint style="info" %}
**Pro Tip:** You can use a [Generate Text Block](/docs/building-ai-agents/blocks-reference/generate-text-block.md) to generate a query for you based on your natural language description. **Make sure to include Schema Info** when using AI-generated queries by providing table and column names to guide query generation.
{% endhint %}

***

### Output Format

Choose how the result should be returned. Options include:

* **JSON** (default): Structured data ideal for parsing or display.
* **CSV:** Comma-separated values, useful for exporting to spreadsheets or displaying flat tabular data.

***

### Output Variable

Set a variable name to store the query result. This variable can be used in later blocks to display results or make decisions.

**Example:**\
`query_result`

***

## How It Works

1. The block connects to the selected external database using the provided credentials.
2. The SQL query is executed (with variables substituted if used).
3. The result is returned in your chosen format and stored in the specified output variable.
4. The next block in your workflow can use this result to generate responses, make decisions, or display information.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://university.mindstudio.ai/docs/building-ai-agents/blocks-reference/query-database.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
