---
schema_version: "1"
id: 93ae162555d877ef68bb5d7e02e4048a8b76e44099f62f6d57fc1b24c35770d0
title: MIN_BY
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/data-cloud/data-cloud-query-guide/references/data-cloud-sql-reference/aggregate-func/min-by.md
canonical_url: https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/min-by.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: b6996d223b4f0d82bf383128c62c3b544792ada10abfbdaf304f6b5c3547d876
products:
  - Data 360 Query Guide
  - Data 360 Developer Documentation
---
# `MIN_BY`

**Applies to:** ✅ Data 360 SQL ✅ Tableau Hyper API

Returns the `<value>` from the row whose `<key>` is smallest among all rows in the aggregate (or within each group when used with `GROUP BY`).

## Syntax

```sql
min_by(<value>, <key>)
```

## Arguments

### Required

- `<value>`: Any expression whose result is returned when its row wins the comparison. The aggregate result type matches this expression’s type.
- `<key>`: The expression `MIN-BY`uses to order rows. The type must support comparison.

## Returns

The `<value>` from the row with the minimum `<key>` among considered rows.

**Null handling:**

- If the aggregate has no input rows (empty table or empty group), the result is `NULL`.
- If every row has a `NULL` key, those rows are skipped; if no row has a non-`NULL` key, the result is `NULL`.
- If the winning row’s `<value>` is `NULL`, the result is `NULL` (the key still determines which row wins).

**Tie breaking:**

When multiple rows share the same minimum key, one of the tied `<value>` results is returned; which tied row wins is implementation-dependent.

## Considerations

- Exactly two arguments are required (`<value>`, `<key>`). Calls with one argument or more than two arguments raise a syntax error.
- You can combine `min_by` with other aggregates, `GROUP BY`, `HAVING`, subqueries, and expressions in the arguments (for example, `max_by(upper(name), score * 2)`).
- `min_by` only supports a single value to sort on. To sort using multiple sort criteria, use [FIRST_VALUE WITHIN GROUP](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/first-value-ordered-set.md).
- Rows with a `NULL` `<key>` are ignored when searching for the minimum key.

## Examples

### Lowest scorer by score

Returns the name and score from the row with the lowest `score`.

```sql
SELECT min_by(name, score) RESPECT NULLS, min(score) AS low_scorer
FROM (
  VALUES
    (1, 'Alice', 85),
    (2, 'Bob', 92),
    (3, 'Charlie', 78),
    (4, 'David', 95),
    (5, 'Eve', 88)
) AS t(id, name, score);
```

Returns `'Charlie'`.

### NULL keys ignored

With keys `5`, `NULL`, `10`, and `15`, the minimum **non-**`NULL` key is `5`, so the result is that row’s `value`.

```sql
SELECT min_by(value, key_val) AS result
FROM (
  VALUES
    (10, 5),
    (20, NULL::int),
    (30, 10),
    (NULL::int, 15)
) AS t(value, key_val);
```

Returns `10`.

### Worst product per region by revenue

```sql
SELECT region, min_by(product, revenue) AS worst_product
FROM (
  VALUES
    ('North', 'A', 100),
    ('North', 'B', 150),
    ('South', 'A', 200),
    ('South', 'B', 120)
) AS sales(region, product, revenue)
GROUP BY region;
```

Returns one row per region. For example `North` / `'A'` and `South` / `'B'`.

## Related Documentation

- [`MAX_BY`](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/max-by.md)
- [`MIN`](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/min.md)
- [Aggregate Functions](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/aggregate.md)
