# Boolean Operators

| AVAILABLE API VERSION |
| --------------------- |
| API v56.0 and later   |

To filter the result set, a query combines several predicates. Boolean operators can help you expand or narrow your query within the `where` filter. Use the `OR` function to combine filters. Use the `AND` function to intersect filters. The `NOT` function inverts a filter.

The filter types have fields for the `AND`, `OR`, and `NOT` functions. `AND` and `OR` take in a list, while `NOT` takes in a single filter to invert. When combined, the filters allow for arbitrary boolean expressions.

```sfdocs-code {"lang":"graphql", "title": "Filter types for boolean expressions", "src": "" }
input Account_Filter {
  and: [Account_Filter]
  or:  [Account_Filter]
  not: Account_Filter
}
```

Each filter within the list of filters is joined by the named operator for `AND` or `OR` functions.

## AND Expressions

Let’s look at a list of filters with the `and` operator.

```sfdocs-code {"lang":"graphql", "title": "Syntax for querying with a list of filters", "src": "" }
where: {
  and: [ {filter1}, {filter2} ]
}
```

Each filter is joined with the `AND` SOQL operator.

```sfdocs-code {"lang":"graphql", "title": "Syntax for SOQL statement with AND operator", "src": "" }
WHERE (filter1) AND (filter2)
```

Here's an example of the previous list of filters.

```sfdocs-code {"lang":"graphql", "title": "", "src": "" }
where: {
  and: [{
    AnnualRevenue: {
      gt: 100000
    }
},
  {
    AnnualRevenue: {
      lt: 1000000
    }
  }]
}
```

Similarly, consider the following filters.

```sfdocs-code {"lang":"graphql", "title": "Syntax for implicit and operator", "src": "" }
where: {
  and: [filter1, filter2, filter3]
}
```

The implicit filter with the `and` operator is similar to a SOQL statement that looks like this.

```sfdocs-code {"lang":"graphql", "title": "Syntax for SOQL statement with AND operator", "src": "" }
WHERE (filter1 AND filter2 AND filter3)
```

Whenever two operators have a value on the filter type, the query combines them with an implicit AND operator.

```sfdocs-code {"lang":"graphql", "title": "Find opportunities that need an estimate in October", "src": "" }
Opportunity(
  where: {
    NextStep: {eq: "Need estimate"}
    CloseDate: {
      CALENDAR_MONTH: {
        value: {
          eq: 10
        }
      }
    }
  }
)
```

This query is similar to the following SOQL statement. It assumes that `Id` and `Name` are included in a `SELECT` clause.

```sfdocs-code {"lang":"sql", "title": "SOQL statement with AND operator", "src": "" }
FROM Opportunity
WHERE NextStep='Need estimate' AND CALENDAR_MONTH(CloseDate)=10
```

Similarly, the following query contains multiple operators `eq` and `gt` with a value on the filter type.

```sfdocs-code {"lang":"graphql", "title": "Find Accounts with annual revenue greater than a million in Tokyo", "src": "" }
Account(
  where: {
    AnnualRevenue: {gt: 1000000}
    Ownership: {eq: "Public" }
    ShippingCity: {
      eq: "Tokyo"
    }
  }
)
```

The previous query is similar to the following SOQL statement. It assumes that `Id` and `Name` are included in a `SELECT` clause.

```sfdocs-code {"lang":"sql", "title": "SOQL statement with multiple AND operators", "src": "" }
SELECT Id, Name
FROM Account
WHERE AnnualRevenue>1000000 AND Ownership='Public' AND ShippingCity='Tokyo'
```

## OR Expressions

Similarly, a filter with the `or` operator looks like this.

```sfdocs-code {"lang":"graphql", "title": "Syntax for or operator", "src": "" }
where: {
  or: [filter1, filter2, filter3]
}
```

The `OR` function is similar to the following SOQL statement.

```sfdocs-code {"lang":"sql", "title": "Syntax for SOQL statement with OR operator", "src": "" }
WHERE (filter1 OR filter2 OR filter3)
```

Let’s look at how to use the `or` filter type.

```sfdocs-code {"lang":"graphql", "title": "Find accounts with annual revenue greater than 10 million or public ownership", "src": "" }
Account(
  where: {
    or: [
        { AnnualRevenue: {gt: 10000000} }
        { Ownership: {eq: "Public"} }
    ]
  }
)
```

The previous query is similar to this SOQL statement with the `OR` operator. It assumes that `Id` and `Name` are included in a `SELECT` clause.

```sfdocs-code {"lang":"sql", "title": "SOQL statement with OR operator", "src": "" }
SELECT Id, Name
FROM Account
WHERE AnnualRevenue>10000000 OR Ownership='Public'
```

## NOT Expressions

The `NOT` function inverts the predicate within the filter.

```sfdocs-code {"lang":"graphql", "title": "Find accounts where the shipping city is not Tokyo", "src": "" }
Account(
  where: {
    not: {
      ShippingCity: {
        eq: { "Tokyo" }
      }
    }
  }
)
```

The previous query is similar to the following SOQL statement with the `NOT` operator. It assumes that `Id` and `Name` are included in a `SELECT` clause.

```sfdocs-code {"lang":"sql", "title": "SOQL statement with NOT operator", "src": "" }
SELECT Id, Name
FROM Account
WHERE NOT ShippingCity='Tokyo'
```

The `NOT` function can be used with the binary functions as well.

```sfdocs-code {"lang":"graphql", "title": "Syntax for not operator with and filters", "src": "" }
where: {
  not: {
    and: [filter1, filter2]
  }
}
```

The previous `NOT` function is similar to the following SOQL statement.

```sfdocs-code {"lang":"graphql", "title": "Syntax for SOQL statement with WHERE NOT clause", "src": "" }
WHERE NOT (filter1 AND filter2)
```

This query uses the `not` filter type with multiple `and` filters.

```sfdocs-code {"lang":"graphql", "title": "Find opportunities that are not closed won and does not have a full probability", "src": "" }
Opportunity(
  where: {
    not: { Probability: { eq: 100 }
           StageName: { eq: "Closed Won" }
    }
  }
)
```

The previous query is similar to the following SOQL statement with the `WHERE NOT` clause.

```sfdocs-code {"lang":"sql", "title": "SOQL statement with the WHERE NOT clause", "src": "" }
SELECT Id, Name
FROM Opportunity
WHERE NOT (Probability=100 AND StageName='Closed Won')
```

## Filter Nesting

Furthermore, we can have arbitrary nesting of filters.

```sfdocs-code {"lang":"graphql", "title": "Syntax for filter nesting", "src": "" }
where: {
  and: [
    {or: [filter11, filter12]},
    {or: [filter21, filter22]}
  ]
}
```

The nesting of filter is similar as follows.

```sfdocs-code {"lang":"graphql", "title": "Syntax for SOQL statement with filter nesting", "src": "" }
WHERE ( (filter11 OR filter12) AND (filter21 OR filter22) )
```

This query uses nesting of filters.

```sfdocs-code {"lang":"graphql", "title": "Find opportunities using filter nesting", "src": "" }
Opportunity(
    where: {
      or: [
        {
          Probability: { eq: 100 },
          StageName: { eq: "Closed Won" }
        },
        {
          Type: { eq: "New Customer" },
          ExpectedRevenue: { gt: 1000000 }
        }
      ]
    }
)
```

The previous query is similar to the following SOQL statement.

```sfdocs-code {"lang":"sql", "title": "SOQL statement with nesting of filters", "src": "" }
SELECT Id, Name
FROM Opportunity
WHERE (Probability=100 AND StageName='Closed Won') OR (Type='New Customer' AND ExpectedRevenue>1000000)
```
