# Executing SQL

Query your time-series data and [metadata](https://dev.nominal.io/platform/data-model.md#data-model-metadata) with [SQL](https://dev.nominal.io/sql-guides.md). Each example lists the datasets in your workspace.

## In code

The Python, Rust, and Go examples call the SQL REST API directly. Python reads the REST API URL, API key, and workspace RID from your `default` profile. For Rust and Go, paste them in from the [API keys setup](https://dev.nominal.io/quickstart/api-keys-setup.md) page.

**Python**

```python
import requests
from nominal.config import NominalConfig

profile = NominalConfig.from_yaml().get_profile("default")

response = requests.post(
    f"{profile.base_url}/sql/v1/query",
    headers={"Authorization": f"Bearer {profile.token}"},
    json={
        "query": "SELECT dataset_rid, name FROM datasets LIMIT 20",
        "workspace_rid": profile.workspace_rid,
        "result_format": "SQL_SERVICE_QUERY_RESULT_FORMAT_CSV",
    },
)
print(response.text)
```

**MATLAB**

```matlab
client = nominal.Client.fromProfile();
t = client.query("SELECT dataset_rid, name FROM datasets LIMIT 20")
```

**Rust**

```rust
// needs the reqwest (with its json feature) and serde_json crates
let api_url = "<api url>";
let api_key = "<api key>"; // DO NOT COMMIT THIS
let workspace_rid = "<workspace rid>";

let csv = reqwest::Client::new()
    .post(format!("{api_url}/sql/v1/query"))
    .bearer_auth(api_key)
    .json(&serde_json::json!({
        "query": "SELECT dataset_rid, name FROM datasets LIMIT 20",
        "workspace_rid": workspace_rid,
        "result_format": "SQL_SERVICE_QUERY_RESULT_FORMAT_CSV",
    }))
    .send().await?
    .text().await?;
println!("{csv}");
```

**Go**

```go
import (
	"bytes"
	"encoding/json"
	"io"
	"net/http"
	"os"
)

const apiURL = "<api url>"
const apiKey = "<api key>" // DO NOT COMMIT THIS
const workspaceRid = "<workspace rid>"

body, _ := json.Marshal(map[string]string{
	"query":         "SELECT dataset_rid, name FROM datasets LIMIT 20",
	"workspace_rid": workspaceRid,
	"result_format": "SQL_SERVICE_QUERY_RESULT_FORMAT_CSV",
})
req, _ := http.NewRequest("POST", apiURL+"/sql/v1/query", bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
resp, _ := http.DefaultClient.Do(req)
io.Copy(os.Stdout, resp.Body)
```

## In the Nominal app

1. In the sidebar, click **Data**, then **SQL**.
2. Under **Start a query**, choose **No scope**, then type the query, for example `SELECT dataset_rid, name FROM datasets LIMIT 20`.
3. Click **RUN**, or press Ctrl+Enter (⌘+Enter on a Mac).

The results show up to 5,000 rows. To download a full result, see [Exporting data by SQL](https://dev.nominal.io/quickstart/quick/quick-sql-export.md).

For the rest of the console, see [🔒︎SQL console](https://docs.nominal.io/developers/sql/console/overview).
