---
schema_version: "1"
id: 747c005b8d99d7744d12a81363f1a54d74ec075b2fbd17bb5e334a78e945c031
title: Order By Schema
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/graphql-api/graphql/guides/aggregate/aggregate-orderby-schema.md
canonical_url: https://developer.salesforce.com/docs/platform/graphql/guide/aggregate-orderby-schema.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 3221f2f527ff1672086e69fb2cc6aec638a096dce7ba9f5d796255cbc0b090fd
products:
  - GraphQL API
---
# Order By Schema

| AVAILABLE API VERSION |
| --------------------- |
| API v59.0 and later   |

All sObjects in the schema have a corresponding `RecordOrderBy` type. The `RecordOrderBy` type combines the object API name with the `_OrderBy` suffix. For example:

```
type Account {
  groupBy: Account_GroupBy
  orderBy: Account_OrderBy
}
```

The `RecordOrderBy` type is introduced by the [`RecordQueryAggregate` type](https://developer.salesforce.com/docs/platform/graphql/guide/aggregate.md#recordqueryaggregate-type).

## RecordOrderBy Type

`RecordOrderBy` creates one input type per sObject type. For example, Account has a corresponding `Account_OrderBy` type that you can use with the `orderBy` argument.

The `Account_OrderBy` type contains fields of `OrderByClause` type. Some fields are shown here for brevity. We recommend that you use the [Altair GraphQL client](https://developer.salesforce.com/docs/platform/graphql/get-started-graphql.md) for a complete view of the schema.

```
input Account_OrderBy {
  AccountNumber: AggregateOrderByStringClause
  AnnualRevenue AggregateOrderByNumberClause
  BillingCity: AggregateOrderByStringClause
  BillingCountry: AggregateOrderByStringClause
  CreatedBy: User_OrderBy
  CreatedById: NoFunctionAggregateOrderByClause
  Id: AggregateOrderByStringClause
  Industry: AggregateOrderByStringClause
  Name: AggregateOrderByStringClause
  NumberOfEmployees: AggregateOrderByNumberClause
  OperatingHours: OperatingHours_OrderBy
  Owner: User_OrderBy
  OwnerId: NoFunctionAggregateOrderByClause
  Ownership: AggregateOrderByStringClause
  Parent: Account_OrderBy
  ParentId: NoFunctionAggregateOrderByClause
}
```

The main types include:

- `AggregateOrderByStringClause` - Defines aggregate functions used by fields of these types:
  - String
  - Id
  - DateTime
  - Time
  - Date
  - Email
  - PhoneNumber
  - Url
  - Picklist
- `AggregateOrderByNumberClause` - Defines aggregate functions used by fields of these types:
  - Int
  - Double
  - Long
  - Latitude
  - Longitude
  - Percent
- `NoFunctionAggregateOrderByClause` - Defines how null values are returned and how sorting order are used by fields of these types:
  - Boolean
  - Reference fields like AccountId and OwnerId

## AggregateOrderByStringClause Type

The `AggregateOrderByStringClause` type has several enumeration types.

```
type AggregateOrderByStringClause {
  function: AggregateOrderByStringFunction
  nulls: NullsOrder
  order: ResultsOrder
}
```

Pass these types to the `orderBy` argument in the `aggregate` field.

- `function` - The aggregate function to apply to the filter.
- `nulls` - Orders null records either first or last.
- `order` - Order the aggregate results by ascending or descending order.

## AggregateOrderByStringFunction Enum

The `AggregateOrderByStringFunction` enum values define aggregate functions for string fields like date, email, or Url and so on.

```
enum AggregateOrderByStringFunction {
  COUNT
  COUNT_DISTINCT
  MAX
  MIN
}
```

Use these values with string fields.

- `COUNT` - Returns the number of rows matching the query criteria.
- `COUNT_DISTINCT` - Returns the number of distinct non-null field values matching the query criteria.
- `MAX` - Returns the maximum value of a field.
- `MIN` - Returns the minimum value of a field.

## AggregateOrderByNumberClause Type

The `AggregateOrderByNumberClause` type has several enumeration types.

```
type AggregateOrderByNumberClause {
  function: AggregateOrderByNumberFunction
  nulls: NullsOrder
  order: ResultsOrder
}
```

Pass these types to the `orderBy` argument in the `aggregate` field.

- `function` - The aggregate function to apply to the filter.
- `nulls` - Orders null records either first or last.
- `order` - Order the aggregate results by ascending or descending order.

## NoFunctionAggregateOrderByClause Type

The `NoFunctionAggregateOrderByClause` type has several enumeration types.

```
type NoFunctionAggregateOrderByClause {
  nulls: NullsOrder
  order: ResultsOrder
}
```

Pass these types to the `orderBy` argument in the `aggregate` field.

- `nulls` - Orders null records either first or last.
- `order` - Order the aggregate results by ascending or descending order.

## AggregateOrderByNumberFunction Enum

The `AggregateOrderByNumberFunction` enum values define aggregate functions for number fields like double, long, percent and so on.

```
enum AggregateOrderByNumberFunction {
  AVG
  COUNT
  COUNT_DISTINCT
  MAX
  MIN
  SUM
}
```

Use these values with numeric fields.

- `AVG` - Returns the average value of a field.
- `COUNT` - Returns the number of rows matching the query criteria.
- `COUNT_DISTINCT` - Returns the number of distinct non-null field values matching the query criteria.
- `MAX` - Returns the maximum value of a field.
- `MIN` - Returns the minimum value of a field.
- `SUM` - Returns the total sum of a numeric field.

## NullsOrder Enum

The `NullsOrder` enumeration type specifies the order in which nulls appear in the result set. By default, null values are sorted first.

```
enum NullsOrder {
  FIRST
  LAST
}
```

Use these values in the `orderBy` argument.

- `FIRST` - Order the null values first. This is the default.
- `LAST` - Order the null values last.

## ResultsOrder Enum

The `ResultsOrder` enumeration type specifies whether the results are ordered in ascending or descending order. By default, the result is ordered in ascending order.

```
enum ResultsOrder {
  ASC
  DESC
}
```

Use these values in the `orderBy` argument.

- `ASC` - Ascending order (default)
- `DESC` - Descending order

## User_OrderBy Type

Some fields like `CreatedBy` and `Owner` correspond to a `_OrderBy` type, which contains additional fields.

```
type User_GroupBy {
  AboutMe: AggregateOrderByStringClause
  Account: Account_OrderBy
  AccountId: NoFunctionAggregateOrderByClause
  Contact: Contact_OrderBy
  NumberOfFailedLogins: AggregateOrderByNumberClause
  # Other User_OrderBy fields
}
```

## See Also

[Aggregate Examples](https://developer.salesforce.com/docs/platform/graphql/guide/aggregate-examples.md)

[SOQL and SOSL Reference: Date Functions](https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_date_functions.htm)
