# Query Filtering

Query filtering applies conditions to determine which records qualify for a query result.

Cloud concept. Updated 2026-11-26. Page: https://blowstack.com/tools/cloud-concepts-hub/query-filtering

> Query filtering selects records by evaluating conditions such as equality, ranges, or membership. Applying those conditions at the data source can reduce data transfer and application work, but the cost of evaluating a filter depends on how the source stores and indexes the data.

Practice exams covering Query Filtering: [DVA-C03 AWS Certified Developer - Associate](https://blowstack.com/practice-exams/aws-certified-developer-associate).

## 5 key aspects of Query Filtering

Reliable query filtering combines documented match rules, validated inputs, efficient source execution, independent authorization, and consistent handling of pages and cached results.

1. **Explicit match semantics**: Define supported operators and how they treat nulls, missing values, case, and range boundaries. For timestamps, inclusive starts and exclusive ends let adjacent time windows meet without overlap.
2. **Typed API filter contracts**: Expose supported fields and operators through documented REST parameters or typed GraphQL inputs. Reject unsupported combinations and bound the size or complexity of filter expressions.
3. **Data-source execution**: Translate filters into native predicates so indexes, partitions, or search-engine structures can reduce the data read. Confirm the savings with query plans or scan metrics rather than inferring them from the response size.
4. **Independent access enforcement**: Treat user filters as requests to narrow the data a caller is already allowed to see. Enforce authorization separately, including when the caller supplies no filter.
5. **Consistent pagination and caching**: Page over the filtered result set and tie cursors to the filter and sort that produced them. Distinguish cached responses by effective query and access scope so different requests cannot reuse an incorrect result.

## Cloud services for Query Filtering

These services apply filtering at different layers. An API layer such as AWS AppSync defines the filter contract and lets resolvers translate client arguments into backend operations. Azure SQL Database illustrates how a relational store executes transactional predicates, while Azure AI Search combines attribute constraints with text retrieval on indexed data. BigQuery illustrates the same principle for analytics, where partition pruning can reduce scanned bytes. An architecture need not use all four: choose the data source that owns the query, push supported conditions into it, and keep filter semantics, pagination, and authorization consistent at the API boundary.

- **[Azure SQL Database](https://blowstack.com/tools/cloud-services-hub/azure-sql-database)**: Azure SQL Database applies predicates through SQL clauses such as WHERE. A REST or GraphQL data layer can translate entity filters into parameterized SQL, while database row-level security can enforce access restrictions independently of those filters.
- **[AWS AppSync](https://blowstack.com/tools/cloud-services-hub/aws-appsync)**: AWS AppSync GraphQL schemas can expose typed filter arguments on fields. Resolvers validate and translate those arguments into conditions appropriate to the configured data source.
- **[Foundry IQ](https://blowstack.com/tools/cloud-services-hub/foundry-iq)**: Azure AI Search supports OData filter expressions on index fields marked as filterable. Applications can combine exact attribute constraints, such as category or availability, with full-text search in the same request.

## What is Query Filtering

Query filtering determines which records qualify for a result. A filter can compare a field to a value, check whether a value falls within a range, or combine conditions with AND and OR. For example, an order service might return only shipped orders created on or after a specified date. Filtering is distinct from projection, which chooses the fields to return, and sorting, which determines their order. Define how filters handle null or missing values, case sensitivity, and date boundaries so callers know what will match.

When exposing an entity through an API, define which fields are filterable, which operators each field supports, and how multiple conditions are combined. A REST endpoint might accept `GET /orders?status=shipped`, while a GraphQL schema might expose a typed `filter` argument on its `orders` field. These are interface conventions, not capabilities that REST or GraphQL provides automatically. Validate field names, operators, and value types, and translate accepted conditions into native predicates with bound values rather than concatenating raw query fragments. A client-supplied filter is not an authorization rule: enforce tenant and row-level restrictions independently, then combine them with the requested conditions.

Push filters to the database, search engine, or analytics engine where practical. An index on tenant, status, and creation time can help a relational engine narrow a scan when the query specifies the tenant and status and supplies a date range. But returning fewer rows does not guarantee that fewer rows were read. An unindexed predicate or a function applied to an indexed field can still trigger a broad scan. Inspect query plans or scanned-data metrics, and add indexes for common filter patterns only when the read benefit justifies the extra storage and write work.

Apply filters before pagination so a page contains matching records rather than whatever survives after a page of unfiltered results is fetched. Use a stable sort with a unique tie-breaker, and reject cursor reuse when the filter or sort changes. Search and filtering can share a query but serve different purposes: a text search might rank products for `running shoes`, while a stock filter excludes unavailable products from those results. When caching a filtered response, key it by the effective filter, sort, page, returned fields, and any authorization scope that changes the response. Cache common combinations selectively; many one-off filter combinations produce few hits, and cached results can become stale when records change.

## Query Filtering exam questions

Real practice exam questions on Query Filtering, with the correct answer and its explanation.

### Question 1 (DVA-C03)

**Question:**

A payment service writes one JSON log document per transaction to a search index. After a release, a developer needs to retrieve transactions from a 10-minute incident window in which the primary payment provider timed out. Each document has a transaction timestamp and an `attempts` array mapped as `nested`. Each attempt has `provider` and `outcome` fields mapped as `keyword`. A transaction in which the primary provider returned `DECLINED` and the backup provider returned `TIMEOUT` must be excluded. A transaction in which the primary provider returned `TIMEOUT` and the backup provider returned `APPROVED` must be included. Which query meets these requirements?

**Correct answer:** Use an Amazon OpenSearch Service query with a range filter on the transaction timestamp and a single `nested` query on `attempts` that requires both `provider = primary` and `outcome = TIMEOUT`.

**Explanation:** The timestamp filter selects transactions in the incident window. Both conditions inside the same nested query must match the same attempt, so a backup provider's timeout cannot be mistaken for a primary provider's timeout.

## Practice Query Filtering in a full exam

Questions on Query Filtering in each practice exam, with every answer option and an explanation for each:

- [DVA-C03](https://blowstack.com/practice-exams/aws-certified-developer-associate): 7 questions

## Learn Query Filtering in lessons

2 short lessons on Query Filtering in 1 certification course.

- DVA-C03: Grouped Aggregation vs. Query Filtering (Premium), https://app.blowstack.com/lessons/grouped-aggregation-vs-query-filtering-dva-c03
- DVA-C03: Query Filtering: predicates for log records (Premium), https://app.blowstack.com/lessons/query-filtering-predicates-for-log-records-dva-c03
