# Queries

### Overview

Midio expressions support a specialized query expression syntax for working with lists of data in a clear, readable, and declarative way.

A query expression is composed of a sequence of clauses that are evaluated top to bottom:

* `from` introduces a variable that iterates over a collection
* `where` filters elements based on a condition (optional)
* `join` combines elements from multiple collections based on a predicate (optional)
* `select` defines the final shape of the result

At minimum, a query must contain a `from` clause and a `select` clause. Additional clauses refine or enrich the data as needed.

***

### Basic Queries (`from` / `select`)

#### Example 1: Transforming values

Convert each number in a list into a new value.

```
from n in [1, 2, 3, 4]
select n * 2
```

***

#### Example 2: Selecting a field from objects

Extract a single field from each object.

```
from u in [
  { email: "a@example.com" },
  { email: "b@example.com" }
]
select u.email
```

***

#### Example 3: Constructing new objects

Build a new object for each element in the collection.

```
from p in [
  { name: "Pen", price: 10 },
  { name: "Notebook", price: 25 }
]
select {
  name: p.name,
  price: p.price
}
```

***

### Filtering with `where`

#### Example 4: Filtering values

Keep only values that satisfy a condition.

```
from n in [5, 12, 30]
where n > 10
select n
```

***

#### Example 5: Filtering objects

Filter objects before selecting a field.

```
from u in [
  { email: "a@example.com", isActive: true },
  { email: "b@example.com", isActive: false }
]
where u.isActive
select u.email
```

***

#### Example 6: Filtering and transforming

Filter objects and return a transformed result.

```
from o in [
  { id: 1, total: 50 },
  { id: 2, total: 200 }
]
where o.total > 100
select {
  id: o.id,
  total: o.total
}
```

***

### Joining Collections (`join`)

#### Example 7: Simple join

**Description:** Combine related data from two collections.

```
from u in [ { id: 1, name: "Alice" } ]
join o in [ { userId: 1, total: 100 } ] 
  on u.id == o.userId
select {
  user: u.name,
  total: o.total
}
```

***

#### Example 8: Join with filtering

Join data and filter the combined result.

```
from u in [ { id: 1, name: "Alice" } ]
join o in [ { userId: 1, total: 1000 } ] 
  on u.id == o.userId
where o.total > 500
select {
  user: u.name,
  total: o.total
}
```

***

#### Example 9: Join with conditions on both sides

Use fields from both collections in the filter.

```
from u in [ { id: 1, name: "Alice", isActive: true } ]
join o in [ { userId: 1, id: 10, total: 1000 } ] 
  on u.id == o.userId
where u.isActive && o.total > 500
select {
  user: u.name,
  orderId: o.id
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.midio.com/midio-docs/guides/expressions/queries.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
