# Querying data

### **Execute a query**

```r
library("redivis")

# Execute any SQL query and read the results
query <- redivis$query("SELECT 1 + 1 AS two, 'foo' AS bar")
query$to_tibble()
# 	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_tibble()
# 	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_reader()
# query$to_arrow_dataset()
# query$to_arrow_dataset()
# query$to_data_frame()
# query$to_data_table()
# query$to_sf_tibble()
```

### **Using dbplyr**

```r
con <- redivis$connect_dbi()
tbl(con, "demo.ghcn_daily_weather_data.stations") |> filter(latitude > 0) |> collect()

# or, scope to a dataset / workflow:

con <- redivis$dataset("demo.ghcn_daily_weather_data")$connect_dbi()
tbl(con, "stations") |> filter(latitude > 0) |> collect()
```

### **Execute a scoped query**

```r
library("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
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
")

query$to_tibble()
```

### Run a query within a Redivis notebook

<pre class="language-r"><code class="lang-r"><strong># In a notebook, all queries are scoped to the current workflow.
</strong># Additionally, the notebooks source table can simply be referenced as _source_
query &#x3C;- redivis$query("SELECT * FROM _source_ LIMIT 10")
</code></pre>
