How to retrieve maximum and minimum values from a field

We have seen count query using Aggregation.

In this post, we will see how to retrieve maximum and minimum values from a field

  • The max/min aggregation can only be performed on numerical fields like Integers and Floats.
  • max function returns the maximum value from a set of values
  • min function returns the minimum value from a set of values
  • There are no filters associated with max and min functions.

We will work with the same schema.

type AggObj {
    intFld: Int
    floatFld: Float
}

Let’s use the same 7 records from the previous post.

mutation {
  upsert(
    values: {
      AggObj: [
        { intFld: 1, floatFld: 1.1 }
        { intFld: 2, floatFld: 1.2 }
        { intFld: 3, floatFld: 1.3 }
        { intFld: 4, floatFld: 1.4 }
        { intFld: 4, floatFld: 1.4 }
        { intFld: 3, floatFld: 1.3 }
        { intFld: 1, floatFld: 1.1 }
      ]
    }
  ) {
    id
  }
}

Sample Query

Let’s find out the maximum and minimum from the floatFld and intFld values.

{
  aggregate {
    aggObj {
      intFld {
        max
        min
      }
      floatFld {
        max
        min
      }
    }
  }
}
#result
{
  "data": {
    "aggregate": {
      "aggObj": {
        "intFld": {
          "max": 4,
          "min": 1
        },
        "floatFld": {
          "max": 1.4,
          "min": 1.1
        }
      }
    }
  }
}

Check the POSTMAN collection for the max and min queries in different programming languages! Click </> and choose the programming language of your choice.
Don’t forget to insert your own Authorization key and Hypi Domain under Headers to test the results!
Run in Postman