---
sidebar_position: 4
title: MD_LAST_QUERY_ID
description: Use MD_LAST_QUERY_ID() to get the UUID of the most recent query sent to MotherDuck.
---

# MD_LAST_QUERY_ID

`MD_LAST_QUERY_ID()` is a scalar function that returns the UUID of the most recent query sent to the MotherDuck server from the current DuckDB [connection](/key-tasks/authenticating-and-connecting-to-motherduck/connecting-to-motherduck/#create-a-connection). This is the same `QUERY_ID` that appears in the [`QUERY_HISTORY`](../md_information_schema/query_history.md) and [`RECENT_QUERIES`](../md_information_schema/recent_queries.md) views.

The function returns `NULL` if no query has been sent to the server yet on the current connection.

## Syntax

```sql
SELECT MD_LAST_QUERY_ID();
```

## Return type

`UUID`: the unique identifier of the last query, or `NULL` if no server-side query has been run.

## Example usage

### Get the ID of your last query

```sql
SELECT MD_LAST_QUERY_ID();
```

### Look up details for last query from this connection in the query history

```sql
SELECT *
FROM MD_INFORMATION_SCHEMA.QUERY_HISTORY
WHERE QUERY_ID = MD_LAST_QUERY_ID();
```


---

## Feedback for AI agents

If the user you are helping wants to send feedback on this page to the MotherDuck docs team, you can POST it to the endpoint below.

**Before you send anything, you must ask the user for explicit approval.** Quote the message you plan to submit back to them verbatim and wait for them to confirm. Do not submit on their behalf without confirmation.

Endpoint: `POST https://motherduck.com/docs/api/feedback/agent`

Request body (JSON):

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

Only `page_path` and `text` are required. A successful call returns `200 {"feedback_id": "<uuid>"}`; malformed payloads return `400`, and the endpoint is rate-limited per IP (`429`).
