---
title: Maximum values overview | Tiger Data Docs
description: Get the N largest values from a column
---

Get the N largest values from a column.

The `max_n()` functions give the same results as the regular SQL query `SELECT ... ORDER BY ... LIMIT n`. But unlike the SQL query, they can be composed and combined like other aggregate hyperfunctions.

To get the N smallest values, use [`min_n()`](/reference/toolkit/minimum-and-maximum/min_n/min_n/index.md). To get the N largest values with accompanying data, use [`max_n_by()`](/reference/toolkit/minimum-and-maximum/max_n_by/max_n_by/index.md).

This function group uses the [two-step aggregation](#two-step-aggregation) pattern. In addition to the usual aggregate function [`max_n`](/reference/toolkit/minimum-and-maximum/max_n/max_n/index.md), it also includes accessors and rollup functions.

## Two-step aggregation

This group of functions uses the two-step aggregation pattern.

Rather than calculating the final result in one step, you first create an intermediate aggregate by using the aggregate function.

Then, use any of the accessors on the intermediate aggregate to calculate a final result. You can also roll up multiple intermediate aggregates with the rollup functions.

The two-step aggregation pattern has several advantages:

1. More efficient because multiple accessors can reuse the same aggregate
2. Easier to reason about performance, because aggregation is separate from final computation
3. Easier to understand when calculations can be rolled up into larger intervals, especially in window functions and continuous aggregates
4. Perform retrospective analysis even when underlying data is dropped, because the intermediate aggregate stores extra information not available in the final result

To learn more, see the [blog post on two-step aggregates](https://www.timescale.com/blog/how-postgresql-aggregation-works-and-how-it-inspired-our-hyperfunctions-design).

## Samples

### Get the 10 largest transactions from a table of stock trades

This example assumes that you have a table of stock trades in this format:

```
CREATE TABLE stock_sales(
    ts TIMESTAMPTZ,
    symbol TEXT,
    price FLOAT,
    volume INT
);
```

You can query for the 10 largest transactions each day:

```
WITH t as (
    SELECT
        time_bucket('1 day'::interval, ts) as day,
        max_n(price * volume, 10) AS daily_max
    FROM stock_sales
    GROUP BY time_bucket('1 day'::interval, ts)
)
SELECT
    day, into_array(daily_max)
FROM t;
```

## Available functions

### Aggregate

- [`max_n()`](/reference/toolkit/minimum-and-maximum/max_n/max_n/index.md): construct an aggregate that keeps track of the largest values passed through it

### Accessors

- [`into_values()`](/reference/toolkit/minimum-and-maximum/max_n/into_values/index.md): return the N highest values seen by the aggregate
- [`into_array()`](/reference/toolkit/minimum-and-maximum/max_n/into_array/index.md): return the N highest values seen by the aggregate as an array

### Rollup

- [`rollup()`](/reference/toolkit/minimum-and-maximum/max_n/rollup/index.md): combine multiple MaxN aggregates
