---
title: "Get the SQL query for a data model element"
method: GET
path: "/v2/dataModels/{dataModelId}/elements/{elementId}/query"
tags: ["Data models"]
---

# Get the SQL query for a data model element

`GET /v2/dataModels/{dataModelId}/elements/{elementId}/query`

## Request

- Base URL: `https://api.sigmacomputing.com`
- URL: `https://api.sigmacomputing.com/v2/dataModels/{dataModelId}/elements/{elementId}/query`
- Auth: HTTP bearer

This endpoint retrieves the underlying SQL query associated with a specific element in a data model. It allows users to view and understand how the data for that element is being processed and transformed, which is useful for debugging, optimization, and educational purposes.

### Usage notes
- Retrieve the **dataModelId** by calling the [/v2/dataModels](https://help.sigmacomputing.com/reference/list-data-models) endpoint.
- Retrieve the **elementId** by calling the [/v2/dataModels/{dataModelId}/elements](https://help.sigmacomputing.com/reference/list-data-model-elements) endpoint.
- To retrieve the query for an element from a version-tagged data model, pass the `tagName` in the query parameters with the tag name. Retrieve the tag name by calling the [/v2/dataModels/{dataModelId}/tags](https://help.sigmacomputing.com/reference/list-data-model-tags) endpoint and using the `tagName` included in the response for a given data model.

### Usage scenarios
- **Development and debugging:** Provides developers and analysts with the ability to retrieve and review the SQL code behind data model elements, facilitating troubleshooting and enhancements.
- **Educational insight:** Useful for training purposes or for users learning to understand the impact of various transformations and filters applied within their Sigma data models.

### Best practices
- Restrict who can retrieve query SQL with [folder and document permissions](https://help.sigmacomputing.com/docs/folder-and-document-permissions). [Column-level security (CLS)](https://help.sigmacomputing.com/docs/column-level-security) still applies to the data returned by those queries.
- Use this endpoint as part of a regular review process to ensure that all queries are optimized and up to date with current data schemas.

## Path parameters

- `dataModelId` string, required
- `elementId` string, required

## Query parameters

- `tagName` string

## Response `200`

The response body.

- DataModelsGetDataModelElementQueryResponse200
  - `elementId` string, required
  - `name` string
  - `sql` string
  - `error` string

---

[API](https://skmtc.dev/sigmacomputing/apis/sigma-rest-api.md) - [All operations](https://skmtc.dev/sigmacomputing/apis/sigma-rest-api/llms.txt) - [OpenAPI document](https://skmtc.dev/sigmacomputing/apis/sigma-rest-api/revisions/fb6036208c49?raw)
