# Queries & tag operations

Canonical: https://interlatent.com/docs/ivy/query-reference

Query saved annotations and recorded per-frame data with SQL, then use the matching clip IDs for tagging or export.

## Available query views

Section: https://interlatent.com/docs/ivy/query-reference#views

`run_query(project_id, sql)` reads saved annotations and recorded data. It does not inspect the video or infer new labels. Each view joins on clip_id. Per-frame columns depend on the imported data; inspect the available fields before writing a telemetry query.

| View | Fields |
| --- | --- |
| episodes | clip_id, name, source, episode_index, fps, duration_s, frame_count, tasks |
| spans | clip_id, track, label, t0, t1, revision |
| events | clip_id, track, label, t, revision |
| scalars | clip_id, track, t, value, revision |
| frames | clip_id plus recorded per-frame fields, such as timestamp, observation.state, and action |

Note: Tags are not a column in these SQL views. Discover existing tags through video metadata, then pass the selected clip_ids to narrow a query. Queries are free within your account’s daily allowance.

## Find episodes with a saved presence label

Section: https://interlatent.com/docs/ivy/query-reference#presence

This query assumes a reviewed span track named object_presence whose positive label is exactly “red cup visible”. Replace the track and label with the values actually saved in your project. DISTINCT returns one row per matching episode, even if it has several matching intervals.

### Presence query

```sql
SELECT DISTINCT e.clip_id, e.name
FROM episodes AS e
JOIN spans AS s ON s.clip_id = e.clip_id
WHERE s.track = 'object_presence'
  AND s.label = 'red cup visible'
ORDER BY e.name
```

Note: No rows means no saved span matched this query. It does not establish that a red cup never appears in the recordings.

## Return matching action intervals

Section: https://interlatent.com/docs/ivy/query-reference#intervals

Return clip_id, t0, and t1 when you need evidence windows for review or trimming. This example finds “grasp” spans longer than two seconds on a track named Subtasks. Track names, label spelling, and case must match the stored values.

### Action interval query

```sql
SELECT clip_id, t0, t1, t1 - t0 AS duration_s
FROM spans
WHERE track = 'Subtasks'
  AND label = 'grasp'
  AND t1 - t0 > 2
ORDER BY clip_id, t0
```

- [Export a matching interval](https://interlatent.com/docs/ivy/trimming)

## Combine conditions on the same episode

Section: https://interlatent.com/docs/ivy/query-reference#conditions

Use EXISTS to require two labels on the same episode. This example selects recordings containing a visible red cup and a failed grasp anywhere in that episode. It does not require those intervals to overlap.

### Combined condition query

```sql
SELECT e.clip_id, e.name
FROM episodes AS e
WHERE EXISTS (
    SELECT 1 FROM spans AS s
    WHERE s.clip_id = e.clip_id
      AND s.track = 'object_presence'
      AND s.label = 'red cup visible'
)
AND EXISTS (
    SELECT 1 FROM spans AS s
    WHERE s.clip_id = e.clip_id
      AND s.track = 'grasp_outcome'
      AND s.label = 'failed grasp'
)
```

Note: For simultaneous conditions, add interval-overlap logic, such as a.t0 < b.t1 AND b.t0 < a.t1. Keep the distinction between “in the same recording” and “at the same time” explicit.

## Filter a measurement or recorded channel

Section: https://interlatent.com/docs/ivy/query-reference#scalars

Scalar tracks contain timestamped measurements. This example selects clips whose saved mean_brightness samples fall below 30. The threshold is an example in grayscale units, not a universal dark-video criterion. Validate thresholds on your own footage.

### Scalar threshold query

```sql
SELECT clip_id, MIN(value) AS minimum_brightness
FROM scalars
WHERE track = 'mean_brightness'
GROUP BY clip_id
HAVING MIN(value) < 30
```

Note: For recorded robot data in frames, quote dotted names: "observation.state". DuckDB list indexing starts at 1, so "observation.state"[1] is the first element. Confirm channel meanings and units from the dataset before applying thresholds.

- [Create a brightness track](https://interlatent.com/docs/ivy/custom-code#measurements)

## Tag query results

Section: https://interlatent.com/docs/ivy/query-reference#tag-results

A result with a clip_id column also returns matching clip IDs for follow-up operations. Pass those IDs to update_video_tags to add or remove labels, up to 1,000 IDs per call. Preserve the exact selection and report missing or unowned IDs as skipped.

### Tag tool calls

```python
# Tool-call examples for your agent, not a standalone Python script.
update_video_tags(
    clip_ids=["CLIP_ID"],
    add=["contains-red-cup", "needs-review"],
)

# After review, preserve the content tag and replace the queue tag.
update_video_tags(
    clip_ids=["CLIP_ID"],
    remove=["needs-review"],
    add=["reviewed"],
)
```

Note: Adding a tag is idempotent. A tag cannot be both added and removed in one call. Tags remain manual labels after later annotation edits.

## Return a filtered project link

Section: https://interlatent.com/docs/ivy/query-reference#result-links

Use get_project_link with the chosen tag so the dashboard opens with that filter already applied. Multiple tags match any listed tag. For an AND selection, compute the intersection first and save a dedicated tag if you need a single reusable filtered link.

### Filtered link tool call

```python
get_project_link(
    project_id="PROJECT_ID",
    tags=["contains-red-cup"],
)
```

- [Find & tag videos walkthrough](https://interlatent.com/docs/ivy/find-and-tag)

## Coverage, pagination, and pending results

Section: https://interlatent.com/docs/ivy/query-reference#limits

Queries return at most 10,000 rows. If truncated is true, narrow the query or aggregate it rather than treating the returned rows as the full selection. A query that is still running returns a job ID; poll get_job with kind="query" at least five seconds apart. Use the returned export_version when recording which annotation snapshot informed a result.

Note: The frames view is available only for recordings with imported per-frame data. Read the query notes for excluded recordings. Missing data, a failed import, and an assessed negative result are different states.
