> For the complete documentation index, see [llms.txt](https://cortex-docs.paloaltonetworks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/max_by.md).

# max\_by

Use the `max_by()` function to return the value of one expression corresponding to the maximum value of another expression within a group.

## Syntax

```sql
max_by(<return_expression>, <sort_expression>)
```

## Parameters

| Name                | Type                              | Required | Description                                                                                                                                             |
| ------------------- | --------------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `return_expression` | any                               | Yes      | The expression whose value is returned. This is the value you want to retrieve.                                                                         |
| `sort_expression`   | integer, float, string, timestamp | Yes      | The expression used to determine the maximum. The row with the highest value of this expression determines which `return_expression` value is returned. |

## Returns

**Type**: Same as `return_expression` type

**Description**: The `max_by()` function returns the value of `return_expression` from the row where `sort_expression` has its maximum value within the group. If the input is `null`, the function may skip null values.

## Usage notes

* **Input type**: XQL doesn't support NaN or infinite values as input and these value types also can not be returned.
* **Aggregation Function**: `max_by()` is an aggregation function and must be used with the `comp` stage or within a grouping context.
* **Null Handling**: Rows where `sort_expression` is `null` are typically ignored when determining the maximum.
* **Tie Breaking**: If multiple rows share the same maximum `sort_expression` value, the function returns the `return_expression` value from one of those rows (the specific row is not guaranteed).
* **Relationship to min\_by()**: The `max_by()` function is the counterpart of `min_by()`, which returns the value corresponding to the minimum.
* **Common Use Cases**: This function is used to find associated values at the point of maximum, such as finding the user with the highest score, the timestamp of the latest event, or the source of the largest transaction.

## Examples

### Example 1: Find the event with the highest numeric value

**Goal**: Retrieve the event ID associated with the maximum numeric value in the dataset.

**XQL code**:

```sql
config timeframe = 1d
| dataset = sample_xql_raw
| comp max_event = max_by(event_id, numeric_value)
| fields max_event
```

**Explanation**: The `max_by()` function finds the row with the highest `numeric_value` and returns the corresponding `event_id`. This is useful for identifying which event had the peak value.

**Output**:

| MAX\_EVENT |
| ---------- |
| 107        |

### Example 2: Find the latest event details per category

**Goal**: For each event category, find the event ID of the most recent event.

**XQL code**:

```sql
config timeframe = 1d
| dataset = sample_xql_raw
| comp latest_event = max_by(event_id, _time) by event_type
| fields event_type, latest_event
| limit 3
```

**Explanation**: This query groups records by `event_type` and for each group, returns the `event_id` of the row with the latest `_time` value. This identifies the most recent event in each category.

**Output**:

| EVENT\_TYPE | LATEST\_EVENT |
| ----------- | ------------- |
| login       | 115           |
| logout      | 112           |
| error       | 108           |

### Example 3: Find the source with the highest duration

**Goal**: Identify which source host had the longest duration event.

**XQL code**:

```sql
config timeframe = 1d
| dataset = sample_xql_raw
| comp longest_source = max_by(source_name, duration_seconds)
| fields longest_source
```

**Explanation**: The `max_by()` function finds the row with the highest `duration_seconds` and returns the corresponding `source_name`, identifying which source experienced the longest event.

**Output**:

| LONGEST\_SOURCE |
| --------------- |
| server-03       |

## Related articles

* **Stages**: [`comp`](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/stages/comp.md), [`fields`](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/stages/fields.md), [`limit`](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/stages/limit.md)
* **Functions**: [`min_by`](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/min_by.md), [`greatest`](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/greatest.md), [`first`](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/first.md), [`last`](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/last.md)


---

# 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://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/max_by.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.
