---
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();
```


---

## 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/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'>"
}
```

`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.
