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

# Order of Filtering

## Order of Filtering and Computation

Every query on Honeydew computes needed metrics and applies user filters based on context.

The context of a query includes the [domain](/docs/domains) it operates on the source it came from ([SQL interface](/docs/integration/sql-interface) or a [Dynamic Dataset](/docs/dynamic-datasets)).

The logical order of applying filters:

1. Source data: as defined in [source tables](/docs/modeling/entities#source-table), including any custom SQL filters
2. Domain [source filters](/docs/domains#source-filters) that filter out data from specific source tables, before any computation
3. Entities and their calculated attributes: including calculated attributes that use [filtered metrics](/docs/calculations/metrics#filtered-metrics) that add their own filter
4. Domain [semantic filters](/docs/domains#semantic-filters) that control accessible data across the semantic layer
5. User query filters:
   * When using the [SQL interface](/docs/integration/sql-interface), filters in the SQL (`WHERE` and `HAVING`)
   * When using [Dynamic Datasets](/docs/dynamic-datasets), filters in the Dynamic Dataset definition
6. Metrics, including filters of [filtered metrics](/docs/calculations/metrics#filtered-metrics) in use

<Tip>
  To get the best performance, filters are pushed down and executed as early as possible without affecting results.

  The engine automatically places the filter execution at the earliest point in the query flow.
</Tip>

### Example

Consider:

1. A semantic schema with `lineitem`, `orders` and `customers` entities
2. A domain set up with:

* Source filters `orders.order_date >= '1990-01-01'` and `lineitem.status = 'F'`
* Semantic filter `customers.c_nationkey = 10`

3. A user that asks for `orders.count` with a query filter of `orders.order_date >= '1996-01-01'`

The following filters will apply logically:

1. Select from `orders` with source filter `orders.order_date >= '1990-01-01'`
2. Select from `customers`
3. Apply semantic filter `customers.c_nationkey = 10`:
   * Filter the `customers` entity and join it to `orders`
   * Note that `orders` gets the filter through its INNER join to filtered `customers`
4. Apply query filter `orders.order_date >= '1996-01-01'`
5. Calculate `orders.count` on the resulting filtered data

Note that while logically this is the order, physically the filter in step (5) `orders.order_date >= '1996-01-01'` will be actually executed at step (1), when selecting from `orders`.

The reason is that it can be safely pushed down earlier in the query execution without affecting results.

<Tip>
  The source filter on `lineitem` did not apply as `lineitem` did not participate in the query and a source filter
  only acts on its source entity when it is needed for producing the result.

  However, the semantic filter for `customers` was applied to the result by adding a join to `customers`.
</Tip>
