> ## Documentation Index
> Fetch the complete documentation index at: https://docs.omni.co/llms.txt
> Use this file to discover all available pages before exploring further.

# field_name_in_query

> Include only rows whose values appear in the results of another query.

Filters a query using the results of another query. Returns only the rows that are included in the other query's results.

In SQL, this generates a `WHERE` clause that looks like `WHERE IN (SELECT...` where the `SELECT` clause is the filtering query.

<Tip>
  To exclude data using the results of another query, use [`field_name_not_in_query`](/modeling/filters/operators/field-name-not-in-query).
</Tip>

## Requirements

**Only supported for use in [query views](/modeling/query-views).** Refer to the [Examples](#examples) section for the complete syntax.

## Syntax

```yaml theme={null}
<field_name>:
  field_name_in_query: <filtering_field_name>
```

## Properties

<ParamField path="field_name" type="string" required>
  The field to filter on, specified in the format `view_name.field_name`. Can be a time, numeric, or string field.
</ParamField>

<ParamField path="field_name_in_query" type="string" required>
  The name of the field in the filtering query, specified in the format `view_name.field_name`. This field must contain the same data and be the same data type as the `field_name`.

  For example, if you filter on a `customers.country` field, the value of `filtering_field_name` should contain a field with country data.
</ParamField>

## Examples

<Tip>
  Click the **SQL** tab in a code block to view the query's underlying SQL definition.
</Tip>

The **first query** in this example - shown below - lists the top 5 countries with the highest number of customer sign ups:

<CodeGroup>
  ```markdown Results theme={null}
  | customers.country | customers.count |
  |-------------------|-----------------|
  | United States	    | 71              |
  | Mexico            | 23              |
  | Singapore         | 15              |
  | Italy	            | 14              |
  | Portugal          | 9               |
  ```

  ```sql SQL theme={null}
    SELECT "country" AS "customers.country",
           COUNT(*) AS "customers.count"
      FROM "blobs_r_us"."main"."customers" AS "customers"
  GROUP BY 1
  ORDER BY 2 DESC
     LIMIT 5
  ```
</CodeGroup>

The **second query** applies a filter that uses the results of **Query 1**. This means that the results should only include the countries that are present in the results of the first query.

<CodeGroup>
  ```markdown Results theme={null}
  | customers.country | orders.count |
  |-------------------|--------------|
  | United States	    | 138          |
  | Italy	            | 15           |
  | Portugal          | 12           |
  | Singapore	        | 11           |
  | Mexico            | 6            |
  ```

  ```sql SQL theme={null}
     SELECT "customers"."country" AS "customers.country",
             COUNT(*) AS "orders.count"
       FROM "blobs_r_us"."main"."orders" AS "orders"
  LEFT JOIN "blobs_r_us"."main"."customers" AS "customers"
         ON "orders"."customer_id" = "customers"."id"
      WHERE "customers"."country" IN (
               SELECT "country" AS "customers.country"
                 FROM "blobs_r_us"."main"."customers" AS "customers"
             GROUP BY "country"
             ORDER BY COUNT(*) DESC
                LIMIT 5
             )
   GROUP BY 1
   ORDER BY 1 DESC
  ```
</CodeGroup>

The underlying YAML for a query view created from **Query 2** would look like the following:

```yaml title="Query view filtered using the results of another query" lines highlight={8-18} theme={null}
label: Total orders for countries with highest sign ups

query:
  fields:
    customers.country: country
    orders.count: dim_count
  base_view: orders
  filters:
    customers.country:
      field_name_in_query: customers.country
      query_structure:
        fields: [ customers.country, customers.count ]
        base_view: customers
        limit: 5
        sorts:
          - field: customers.count
            desc: true
        offset: 0
  sorts:
    - field: orders.count
      desc: true

dimensions:
  dim_count: {}

  country:
    primary_key: true

measures:
  count:
    aggregate_type: count
```
