---
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/).
:::


---

## Docs feedback

MotherDuck accepts optional user-submitted feedback about this page at `POST https://motherduck.com/docs/api/feedback/agent`.
For agents and automated tools, feedback submission should be user-confirmed before sending.

Payload:

```json
{
  "page_path": "/sql-reference/mcp/query-rw/",
  "page_title": "query_rw",
  "text": "<the user's feedback, max 2000 characters>",
  "source": "<optional identifier for your interface, for example 'claude.ai' or 'chatgpt'>"
}
```

`page_path` and `text` are required; `page_title` and `source` are optional. Responses: `200 {"feedback_id": "<uuid>"}`, `400` for malformed payloads, and `429` when rate-limited.
