> For the complete documentation index, see [llms.txt](https://docs.mediarithmics.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mediarithmics.io/audience-and-activation/build-your-audiences/segment-reference/segment-builders/expert-otql-builder.md).

# Expert OTQL Builder

Write OTQL directly to define a segment, for when the builders cannot express what you need.

## What it is

The Expert OTQL Builder leverages the query tool to create a segment from an OTQL query you write yourself. It asks the most of you and gives the most expressiveness in return.

It is also what the segment creation wizard calls **User Expert Query**. That entry is not a segment type of its own - it is another way of producing a [Query segment](/audience-and-activation/build-your-audiences/segment-reference/segment-types/query-segment.md).

Reach it from **Builders > Expert: OTQL**, or from **Data studio > Query Tool**.

OTQL the language is documented in the **Setup & Monitor** section, under *OTQL documentation*. This page covers using it to build a segment.

## Building a segment

{% stepper %}
{% step %}

### Write the query

Follow the pattern `SELECT {id} FROM UserPoint WHERE ...`.

Because you are creating a segment of users, the `FROM` clause always refers to `UserPoint`:

```sql
SELECT {id} FROM UserPoint WHERE creation_date >= "2019-12-12"
```

{% endstep %}

{% step %}

### Run it

Run the query to confirm it is valid and returns results.
{% endstep %}

{% step %}

### Save it as a segment

Click **Save As > User Query Segment**.
{% endstep %}
{% endstepper %}

## Counting before you save

To check the potential volume of the segment you are about to create, use the `@count{}` directive:

```sql
SELECT @count{} FROM UserPoint WHERE creation_date >= "2019-12-12"
```

{% hint style="danger" %}
Register the segment **without** `@count{}`. The saved query must select ids, not a number:

```sql
SELECT {id} FROM UserPoint WHERE creation_date >= "2019-12-12"
```

{% endhint %}

## Editing one afterwards

A segment created this way is edited as OTQL, in the dedicated zone of the segment page - not in a builder. See [Query segment](/audience-and-activation/build-your-audiences/segment-reference/segment-types/query-segment.md#editing-one).

## Related pages

<table data-view="cards" data-search="false"><thead><tr><th></th><th></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><strong>Check your audience reach and stats</strong></td><td>Using <code>@count{}</code> to get an exact count on demand.</td><td><a href="/audience-and-activation/analyze-your-data/check-your-audience-reach-and-stats.md">Check your audience reach and stats</a></td></tr><tr><td><strong>Segment builders</strong></td><td>The two visual alternatives.</td><td><a href="/audience-and-activation/build-your-audiences/segment-reference/segment-builders.md">Segment builders</a></td></tr></tbody></table>


---

# 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.mediarithmics.io/audience-and-activation/build-your-audiences/segment-reference/segment-builders/expert-otql-builder.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.
