> ## Documentation Index
> Fetch the complete documentation index at: https://onetest.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# OQL Syntax Reference

> Complete OneTest Query Language syntax documentation

Complete syntax reference for OneTest Query Language (OQL).

## Query Structure

```oql theme={null}
<filter_expression> [ORDER BY <field> [ASC|DESC]] [LIMIT <n>] [OFFSET <n>]
```

All parts except the filter expression are optional.

## Operators

### Comparison Operators

| Operator | Description | Example |
| - | - | - |
| `=` | Equals | `status = active` |
| `!=` | Not equals | `priority != p4` |
| `>` | Greater than | `version > 2.0` |
| `>=` | Greater or equal | `created_at >= 2024-01-01` |
| `<` | Less than | `priority < p2` |
| `<=` | Less or equal | `updated_at <= -7d` |
| `IS NULL` | Field is null | `last_run IS NULL` |
| `IS NOT NULL` | Field is not null | `folder_id IS NOT NULL` |
| `BETWEEN` | Value in range | `created_at BETWEEN 2024-01-01 AND 2024-12-31` |

### String Pattern Operators

| Operator | Description | Example |
| - | - | - |
| `~` | Contains (case-insensitive) | `title ~ "login"` |
| `!~` | Does not contain | `description !~ "deprecated"` |
| `^` | Starts with | `identifier ^ "TC-10"` |
| `$` | Ends with | `title $ "test"` |

### List Operators

| Operator | Description | Example |
| - | - | - |
| `IN` | Value in list | `priority IN (p1, p2, p3)` |
| `NOT IN` | Value not in list | `status NOT IN (archived, deleted)` |

### Array Operators

| Operator | Description | Example |
| - | - | - |
| `CONTAINS` | Array contains value | `tags CONTAINS "smoke"` |
| `NOT CONTAINS` | Array does not contain | `tags NOT CONTAINS "deprecated"` |
| `CONTAINS ANY` | Array contains any value | `tags CONTAINS ANY (smoke, regression)` |
| `CONTAINS ALL` | Array contains all values | `tags CONTAINS ALL (smoke, critical)` |

### Logical Operators

| Operator | Description | Example |
| - | - | - |
| `AND` | Both conditions must be true | `status = active AND priority = p1` |
| `OR` | Either condition must be true | `priority = p1 OR priority = p2` |
| `NOT` | Negate condition | `NOT status = archived` |
| `( )` | Group conditions | `(status = active OR status = approved) AND priority = p1` |

## Data Types

### Strings

Enclose in double or single quotes:

```oql theme={null}
title = "User Login"
title = 'User Login'
```

> [!TIP]
>
> Use quotes for strings with spaces or special characters.

### Numbers

No quotes needed:

```oql theme={null}
version = 2
priority_number > 1
```

### Booleans

```oql theme={null}
automated = true
is_flaky = false
```

### Dates

> [tab] ISO Format
>
> ```oql theme={null}
> created_at = 2024-01-15
> created_at = "2024-01-15T10:30:00Z"
> ```

> [tab] Relative Dates
>
> ```oql theme={null}
> created_at >= -7d        # 7 days ago
> created_at >= -2w        # 2 weeks ago
> created_at >= -3M        # 3 months ago
> created_at >= -1y        # 1 year ago
> ```
>
> Units: `d` (days), `w` (weeks), `M` (months), `y` (years)

> [tab] Date Functions
>
> ```oql theme={null}
> created_at >= startOfWeek()
> created_at >= startOfMonth()
> created_at >= startOfQuarter()
> created_at >= startOfYear()
> created_at <= endOfWeek()
> created_at <= endOfMonth()
> created_at <= endOfQuarter()
> created_at <= endOfYear()
> ```

### Null Values

```oql theme={null}
last_run IS NULL
description IS NOT NULL
```

## Field Names

Field names are case-sensitive:

```oql theme={null}
✅ status = active
❌ Status = active
❌ STATUS = active
```

For fields with spaces or special characters, use quotes:

```oql theme={null}
"custom field" = "value"
```

## Ordering

> [tab] Single Field
>
> ```oql theme={null}
> ORDER BY created_at DESC
> ORDER BY priority ASC
> ORDER BY title
> ```
>
> Default order is `ASC` (ascending).

> [tab] Multiple Fields
>
> ```oql theme={null}
> ORDER BY priority ASC, created_at DESC
> ORDER BY status, priority, title
> ```
>
> Sorts by first field, then second, etc.

## Pagination

```oql theme={null}
# First 50 results
LIMIT 50

# Next 50 results
LIMIT 50 OFFSET 50

# Third page (rows 101-150)
LIMIT 50 OFFSET 100
```

> [!NOTE]
>
> `OFFSET` requires `LIMIT` to be specified.

## Examples by Complexity

> [tab] Simple
>
> ```oql theme={null}
> # Single condition
> status = active
>
> # With ordering
> status = active ORDER BY created_at DESC
>
> # With limit
> priority = p1 LIMIT 10
> ```

> [tab] Moderate
>
> ```oql theme={null}
> # AND condition
> status = active AND priority IN (p1, p2)
>
> # OR condition
> priority = p1 OR tags CONTAINS "critical"
>
> # Pattern matching
> title ~ "login" AND status != archived
>
> # Date filtering
> created_at >= -30d ORDER BY created_at DESC LIMIT 100
> ```

> [tab] Complex
>
> ```oql theme={null}
> # Multiple conditions with grouping
> (status = active OR status = approved) AND
> priority IN (p0, p1, p2) AND
> tags CONTAINS ANY (smoke, regression) AND
> created_at >= -90d
> ORDER BY priority ASC, created_at DESC
> LIMIT 50
>
> # Array operations
> tags CONTAINS ALL (smoke, critical) AND
> tags NOT CONTAINS "deprecated" AND
> last_run >= -7d
>
> # Null checks and comparisons
> last_run IS NOT NULL AND
> last_run < updated_at AND
> status = active
> ```

## Operator Precedence

From highest to lowest:

1. `( )` - Parentheses
2. `NOT` - Logical NOT
3. `=, !=, >, >=, <, <=, ~, ^, $, IN, CONTAINS` - Comparison operators
4. `AND` - Logical AND
5. `OR` - Logical OR

> [!TIP]
>
> Use parentheses to make precedence explicit: `(A OR B) AND C`

## Common Patterns

> [accordion] Find by Multiple Tags
>
> ```oql theme={null}
> # Has all these tags
> tags CONTAINS ALL (smoke, critical, login)
>
> # Has any of these tags
> tags CONTAINS ANY (smoke, regression)
>
> # Has this tag but not that tag
> tags CONTAINS "smoke" AND tags NOT CONTAINS "deprecated"
> ```

> [accordion] Date Ranges
>
> ```oql theme={null}
> # This month
> created_at >= startOfMonth()
>
> # Last 7 days
> created_at >= -7d
>
> # Specific range
> created_at BETWEEN 2024-01-01 AND 2024-12-31
>
> # Before a date
> created_at < 2024-06-01
> ```

> [accordion] Multiple Priorities
>
> ```oql theme={null}
> # Any of these priorities
> priority IN (p0, p1, p2)
>
> # Higher priority than P2
> priority < p2
>
> # Exclude low priorities
> priority NOT IN (p3, p4)
> ```

> [accordion] Text Search
>
> ```oql theme={null}
> # Contains word
> title ~ "login"
>
> # Starts with
> identifier ^ "TC-10"
>
> # Ends with
> title $ "test"
>
> # Multiple words (any)
> title ~ "login" OR title ~ "authentication"
>
> # Multiple words (all)
> title ~ "login" AND description ~ "valid credentials"
> ```

## Validation Rules

> [!WARNING]
>
> * Field names must exist in the schema
> * Operators must match field types (can't use `~` on numbers)
> * Date formats must be valid ISO 8601
> * Array operators only work on array fields
> * Priority values must be valid (p0-p4)

## Error Messages

Common errors and how to fix them:

| Error | Cause | Fix |
| - | - | - |
| `Unknown field: xyz` | Field doesn't exist | Check [field reference](https://onetest.ai/oql/field-reference.md) |
| `Invalid operator for field type` | Wrong operator for field | Use comparison operators for numbers, pattern operators for strings |
| `Invalid date format` | Bad date syntax | Use ISO 8601: `2024-01-15` or relative: `-7d` |
| `Expected AND/OR` | Missing logical operator | Add `AND` or `OR` between conditions |
| `Unclosed quote` | Missing closing quote | Ensure all strings have matching quotes |

## What's Next?

> [card] [Query Examples](https://onetest.ai/oql/examples.md)
>
> Real-world OQL examples

> [card] [Field Reference](https://onetest.ai/oql/field-reference.md)
>
> All searchable fields


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.