# Query

## *class* <mark style="color:purple;">Query</mark>

Used to execute a SQL query against table(s) in Redivis, using the [Redivis SQL query syntax](https://docs.redivis.com/reference/projects/transform-nodes/raw-sql), and read out the results.

## Constructors

<table data-header-hidden><thead><tr><th width="312">Method</th><th>Description</th></tr></thead><tbody><tr><td><a href="/pages/mV3ou8rQ0tNBYphLkvhj"><strong><code>redivis.query</code></strong></a>(query_string)</td><td>Execute a SQL query within the <a href="/pages/-Ma_x8sDD20E-u1Y_WVz#environment-variables">current default scope</a> (either a dataset or workflow). <br><br>In a Redivis notebook, the default scope will always be the notebook's workflow, and the notebook's source table can be referenced via the <code>_source_</code> identifier. <br><br>If no default scope is specified, all tables in the query must be fully qualified. Consult the <a href="/pages/-LzsPrAlHWxazTnhQQJE">referencing resources</a> documentation to learn more. </td></tr><tr><td><a href="/pages/ezf6V3pNDLBUf513M2v2"><strong><code>Dataset.query</code></strong></a>(query_string)</td><td>Execute a SQL query scoped to a specific <a href="/pages/Dywj4bMFbhUO1SNVcuJV">dataset</a>. Tables referenced by the query do not need to be fully qualified, since the table lookup is already scoped to the dataset.<br><br>Consult the <a href="/pages/-LzsPrAlHWxazTnhQQJE">referencing resources</a> documentation to learn more. </td></tr><tr><td><a href="/pages/8NWPTksnMlbWw68Dtnva"><strong><code>Workflow.query</code></strong></a>(query_string)</td><td>Execute a SQL query scoped to a specific workflow. Tables referenced by the query do not need to be fully qualified, since the table lookup is already scoped to the dataset.<br><br>Consult the <a href="/pages/-LzsPrAlHWxazTnhQQJE">referencing resources</a> documentation to learn more. </td></tr></tbody></table>

## Examples

{% tabs %}
{% tab title="Basics" %}

```python
# Execute any SQL query and read the results
query = redivis.query("SELECT 1 + 1 AS two, 'foo' AS bar")
query.to_pandas_dataframe()
# 	two	bar
# 0	2	foo

# The query can reference any table on Redivis 
query = redivis.query("""
    SELECT * 
    FROM demo.iris_species.iris 
    WHERE SepalLengthCm > 5
""")
query.to_pandas_dataframe()
# 	Id	SepalLengthCm	SepalWidthCm	PetalLengthCm	PetalWidthCm	Species
# 0	33	5.2	        4.1	        1.5	        0.1	        Iris-setosa
# ...

# Other methods to read data:
# query.to_arrow_batch_iterator()
# query.to_arrow_dataset()
# query.to_arrow_dataset()
# query.to_geopandas_dataframe()
# query.to_dask_dataframe()
# query.to_polars_lazyframe()
```

{% endtab %}

{% tab title="Scoped queries" %}

```python
# To simplify table references, execute a query scoped to a dataset or workflow
dataset = redivis.organization("Demo").dataset("CMS 2014 Medicare Data")
query = dataset.query("""
    SELECT 
        hospice_providers.name, 
        inpatient_charges.drg_definition
    -- The tables inpatient_chargers, hospice_providers are assumed to be 
    -- within the scoped dataset
    FROM inpatient_charges
    INNER JOIN hospice_providers 
        ON hospice_providers.provider_id = inpatient_charges.provider_id
""")

# In a notebook, all queries are scoped to the current workflow.
# Additionally, the notebooks source table can simply be referenced as _source_
query = redivis.query("SELECT * FROM _source_ LIMIT 10")
```

{% endtab %}
{% endtabs %}

## Attributes

| **`properties`** | A dict containing the [API resource representation of the query](/api/resource-definitions/query.md). This will always be populated after the query has been created, and can be refreshed by calling `query.get()` |
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

## Methods

<table data-header-hidden><thead><tr><th width="441"></th><th></th></tr></thead><tbody><tr><td><a href="/pages/7rETab3N7Jli2URaSJ9V"><strong><code>Query.file</code></strong></a>(name, *, [...])</td><td>Reference a <a href="/pages/ogQSUiZGBgXgw0sbRovQ">File</a> within query results that contain a file_id variable.</td></tr><tr><td><a href="/pages/NRHfJq226Adu20BqI7pw"><strong><code>Query.get</code></strong></a>()</td><td>Fetch query metadata. Once called, the <code>properties</code> attribute on the query will be fully populated.</td></tr><tr><td><a href="/pages/w2FfkwPQSE9WpfaspqiQ"><strong><code>Query.list_files</code></strong></a>([max_results, *, ...])</td><td>Return a list of <a href="/pages/ogQSUiZGBgXgw0sbRovQ">File</a> instances for query results containing a file_id variable.</td></tr><tr><td><a href="/pages/uiC2PiNuiGTLEId4zOm0"><strong><code>Query.list_variables</code></strong></a>([max_results, *, ...])</td><td>Return a list of <a href="/pages/sIardnXGFnVA5S0B7sVi">Variable</a> instances associated with this query's output.</td></tr><tr><td><a href="/pages/wp3hozPd3El8OorQFyhj"><strong><code>Query.to_*</code></strong></a>([max_results, *, ...])</td><td>Various methods to read query results. Mirrors the various <a href="/pages/RTo6J5q5TcwH3Rn34708">Table.to_*</a> methods (e.g., <code>table.to_pandas_dataframe()</code></td></tr><tr><td><a href="/pages/rgs1y0qJB47y2HslHTl0"><strong><code>Query.variable</code></strong></a>(name)</td><td>Reference a <a href="/pages/sIardnXGFnVA5S0B7sVi">Variable</a> within the query's output.</td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.redivis.com/api/client-libraries/redivis-python/reference/query.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
