---
sidebar_position: 6.5
title: query_rw
description: Execute SQL queries that can modify data or schema in MotherDuck
---

# query_rw

Execute SQL queries that can modify data or schema in MotherDuck.

## Description

The `query_rw` tool executes SQL against your MotherDuck databases, including operations that change data or schema. For cross-database queries, use fully qualified names: `database.schema.table` (or `database.table` for the main schema).

## Input parameters

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `database` | string | No | Database context for the query. Required when the statement targets database objects. Optional for account-level operations. |
| `sql` | string | Yes | DuckDB SQL statement to execute |

## Output schema

Same as [`query`](/sql-reference/mcp/query/):

```json
{
  "success": boolean,
  "columns": string[],           // Column names (on success)
  "columnTypes": string[],       // Column types (on success)
  "rows": any[][],               // Query results (on success)
  "rowCount": number,            // Number of rows returned (on success)
  "error": string,               // Error message (on failure)
  "errorType": string            // Error type (on failure)
}
```

## Limits

- **Result limit:** Maximum 2,048 rows and 50,000 characters. Results exceeding these limits will be truncated with a truncation message.
- **Query timeout:** 55 seconds. Queries exceeding this limit will be cancelled server-side and the tool will respond with an error message.

## Example usage

**Insert rows:**

```text
Insert a new customer 'Acme Corp' with id 100 into my_database.customers
```

```json
{
  "database": "my_database",
  "sql": "INSERT INTO customers (id, name) VALUES (100, 'Acme Corp')"
}
```

**Update and delete:**

```text
In my_database, set status to 'shipped' for all orders in the orders table where status is 'pending', then delete the old log entries from audit_log
```

The AI assistant can call `query_rw` with the appropriate UPDATE and DELETE statements (or multiple calls if the client requires one statement per call).

**Create table:**

```text
Create a table my_database.main.events with columns id (BIGINT), name (VARCHAR), created_at (TIMESTAMP)
```

```json
{
  "database": "my_database",
  "sql": "CREATE TABLE main.events (id BIGINT, name VARCHAR, created_at TIMESTAMP)"
}
```

**Account-level operations (database optional):**

```text
Create a new database called reporting
```

```json
{
  "sql": "CREATE DATABASE reporting"
}
```

For account-level operations, omit `database` and pass only `sql`.

:::tip Read-only access
To restrict the MCP server so the AI can only read data, see [Restricting to read-only access](/key-tasks/ai-and-motherduck/securing-read-only-access/).
:::
