> For the complete documentation index, see [llms.txt](https://v2.dataos.info/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://v2.dataos.info/references/interfaces/apis/data-product-apis/querying-data-products/advanced/source-sql.md).

# Source SQL

Use the physical data layer only for questions beyond the semantic model.

Source SQL runs warehouse `SELECT` statements against physical tables. Use it for direct warehouse access when the semantic layer does not expose the fields or expressions you need.

**Endpoint:** `POST /api/v1/query/statement`

```json
{
  "sql": "SELECT store_region, COUNT(*) FROM retail.orders WHERE status = 'delivered' GROUP BY 1 LIMIT 100",
  "params": {},
  "meta": {}
}
```

Rules:

* **`SELECT` only.** No INSERT, UPDATE, DELETE, or DDL.
* Tables must exist in the active environment. Use Physical Table names from metadata (`model.table.physical.name`).
* Write SQL in the gateway warehouse dialect (`metadata.gateway.dialect`, for example `postgres`). The API does not translate dialects for Source SQL.
* Optional `?gateway=` query parameter when multiple gateways are configured.

### Response lifecycle

`POST /api/v1/query/statement` with `{ "sql": "...", "params": {}, "meta": {} }` returns `202 Accepted` and a statement `id`. Poll `GET /api/v1/query/statement/{id}` until `status` is `SUCCESS` or `FAILED`. Fetch rows from `GET /api/v1/query/statement/{id}/result?format=json` only after `SUCCESS`.

Status codes, deduplication, and export formats: see [Query lifecycle](https://v2.dataos.info/references/interfaces/apis/data-product-apis/querying-data-products/advanced/query-lifecycle.md).

Use Source SQL for ad-hoc access to Physical Tables, columns not exposed on the semantic layer, or expressions the semantic transpiler will not accept. Use the semantic layer when you need governed measures, segments, or cross-model joins.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://v2.dataos.info/references/interfaces/apis/data-product-apis/querying-data-products/advanced/source-sql.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
