> 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/examples/querying-data.md). Agents can also search and read this documentation through its [MCP server](https://docs.redivis.com/~gitbook/mcp).

# Querying data

### **Execute a query**

```javascript
import redivis from 'redivis';

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

### **Execute a scoped query**

```javascript
import redivis from 'redivis';

// Perform a query on the Demo CMS Medicare data.
// Table at https://redivis.com/datasets/349j-7phs91amz/tables

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

const rows = await query.toRowsArray(100);
```

### Set a default scope

```javascript
import redivis from 'redivis';

// Unqualified table references in all queries will be resolved within this workflow
redivis.config({ defaultWorkflow: 'jane.my_workflow' });

const rows = await redivis.query('SELECT * FROM my_output_table LIMIT 10').toRowsArray();
```

### Stream large query results

```javascript
import redivis from 'redivis';

const query = redivis.query(`
  SELECT FIREYEAR, GISACRES
  FROM demo.us_forest_service_fires.us_fire_perimeters
`);

let totalAcres = 0;
for await (const batch of query.toArrowBatchIterator()) {
  for (const acres of batch.getChild('GISACRES')) {
    totalAcres += acres ?? 0;
  }
}
```

### Check a query's status

```javascript
import redivis from 'redivis';

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

await query.get(); // Starts the query, if it hasn't been started yet
console.log(query.properties.status); // e.g., 'running'

try {
  await query.toRowsArray(); // Waits for the query to finish
} catch (e) {
  if (e instanceof redivis.errors.JobError) {
    console.error('The query failed:', e.message);
  } else {
    throw e;
  }
}
```


---

# 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/examples/querying-data.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.
