> For the complete documentation index, see [llms.txt](https://docs.redivis.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.redivis.com/api/client-libraries/redivis-js/reference/query.md).

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

Queries are lazy: constructing a query doesn't run it. The query is run the first time its results or metadata are needed (e.g., when calling `query.toRowsArray()` or `query.get()`), and only once, however many times its results are then read. Query results can be read for up to 24 hours after the query completes.

## Constructors

| Method                                                                                                                                | Description                                                                                                                                                                                                                                                                                                                                                                                      |
| ------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| [**`redivis.query`**](https://docs.redivis.com/api/client-libraries/redivis-js/reference/redivis/redivis.query.md)(query\[, options]) | Construct a SQL query within the [current default scope](https://docs.redivis.com/api/client-libraries/redivis-js/reference.md#environment-variables) (either a dataset or workflow). If no default scope is specified, all tables in the query must be fully qualified. Consult the [referencing resources](https://docs.redivis.com/api/referencing-resources.md) documentation to learn more. |
| [**`Dataset.query`**](https://docs.redivis.com/api/client-libraries/redivis-js/reference/dataset/dataset.query.md)(query)             | Construct a SQL query scoped to a specific [dataset](https://docs.redivis.com/api/client-libraries/redivis-js/reference/dataset.md). Tables referenced by the query do not need to be fully qualified, since the table lookup is already scoped to the dataset.                                                                                                                                  |
| [**`Workflow.query`**](https://docs.redivis.com/api/client-libraries/redivis-js/reference/workflow/workflow.query.md)(query)          | Construct a SQL query scoped to a specific [workflow](https://docs.redivis.com/api/client-libraries/redivis-js/reference/workflow.md). Tables referenced by the query do not need to be fully qualified, since the table lookup is already scoped to the workflow.                                                                                                                               |

## Examples

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

```javascript
// Execute any SQL query and read the results
const query = redivis.query("SELECT 1 + 1 AS two, 'foo' AS bar");
await query.toRowsArray();
// [{ two: 2, bar: 'foo' }]

// The query can reference any table on Redivis
const irisQuery = redivis.query(`
  SELECT *
  FROM demo.iris_species.iris
  WHERE SepalLengthCm > 5
`);
await irisQuery.toRowsArray();
// [{ Id: 33, SepalLengthCm: 5.2, SepalWidthCm: 4.1, PetalLengthCm: 1.5, PetalWidthCm: 0.1, Species: 'Iris-setosa' }, ...]

// Other methods to read data:
// await query.toArrowTable()
// query.toArrowBatchIterator()
// await query.toReadStreams()
```

{% endtab %}

{% tab title="Scoped queries" %}

```javascript
// To simplify table references, execute a query scoped to a dataset or workflow
const dataset = redivis.organization('demo').dataset('cms_2014_medicare_data');
const query = dataset.query(`
  SELECT
    hospice_providers.name,
    inpatient_charges.drg_definition
  -- The tables inpatient_charges and 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
`);
```

{% endtab %}

{% tab title="Query metadata" %}

```javascript
const query = redivis.query('SELECT * FROM demo.iris_species.iris');

await query.get(); // Runs the query, and fetches its status
console.log(query.properties.status, query.properties.outputNumRows);

const variables = await query.listVariables(); // Waits for the query to finish
```

{% endtab %}
{% endtabs %}

## Attributes

| Name             | Description                                                                                                                                                                                                                         |
| ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`properties`** | An object containing the [API resource representation of the query](https://docs.redivis.com/api/resource-definitions/query.md). This will be populated once the query has been run, and can be refreshed by calling `query.get()`. |

## Methods

| Method                                                                                                                                       | Description                                                                                                                                                                                                |
| -------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [**`Query.download`**](https://docs.redivis.com/api/client-libraries/redivis-js/reference/table/table.download.md)(\[path, options])         | Export the query's results in a particular format and download them to disk (Node.js only).                                                                                                                |
| [**`Query.file`**](https://docs.redivis.com/api/client-libraries/redivis-js/reference/table/table.file.md)(path)                             | Return a [File](https://docs.redivis.com/api/client-libraries/redivis-js/reference/file.md) within query results that contain a `file_id` variable.                                                        |
| [**`Query.get`**](https://docs.redivis.com/api/client-libraries/redivis-js/reference/query/query.get.md)()                                   | Run the query (if it hasn't been run yet), and fetch its metadata. Once called, the `properties` attribute on the query will be populated.                                                                 |
| [**`Query.listFiles`**](https://docs.redivis.com/api/client-libraries/redivis-js/reference/table/table.listfiles.md)(\[maxResults, options]) | Return a list of [File](https://docs.redivis.com/api/client-libraries/redivis-js/reference/file.md) instances for query results containing a `file_id` variable.                                           |
| [**`Query.listVariables`**](https://docs.redivis.com/api/client-libraries/redivis-js/reference/query/query.listvariables.md)(\[maxResults])  | Return a list of [Variable](https://docs.redivis.com/api/client-libraries/redivis-js/reference/variable.md) instances associated with the query's output.                                                  |
| [**`Query.to*`**](https://docs.redivis.com/api/client-libraries/redivis-js/reference/query/query.to.md)(\[maxResults, options])              | Various methods to read the query's results. Mirrors the various [`Table.to*`](https://docs.redivis.com/api/client-libraries/redivis-js/reference/table.md#methods) methods (e.g., `query.toRowsArray()`). |
| [**`Query.variable`**](https://docs.redivis.com/api/client-libraries/redivis-js/reference/query/query.variable.md)(name)                     | Return a [Variable](https://docs.redivis.com/api/client-libraries/redivis-js/reference/variable.md) within the query's output.                                                                             |


---

# 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://docs.redivis.com/api/client-libraries/redivis-js/reference/query.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.
