# Query Predicates In our [Manage resources with the SDK](/learning-developer-essentials/manage-resources-with-the-sdk/overview.md) module, we learned how to perform Create, Read, Update, and Delete (CRUD) operations on resources. We will build on this knowledge in this module and focus on how to query resources based on specific conditions. Let’s take an example. Say you want to get the data on all customers in the age group 30 to 40 who reside in Berlin and have purchased products from a particular category in the last year. This is where query requests come in to help. By using [API queries](/api/general-concepts.md#query-features) with appropriate predicates, you can retrieve a subset of data based on specific criteria. When used properly, queries can greatly enhance the performance and user experience of your application. [Query Predicates](/api/predicates/query.md) are a domain-specific language designed to define complex expressions for querying resources. The syntax consists of field identifiers, operators, and values. Here's a breakdown of the syntax: - **Field identifiers**: these reference specific fields within a resource. For example, `totalPrice` or `customerEmail`. - **Operators**: these are used to compare the field identifier with a value. Common operators include `=`, `!=`, `>`, `<`, `in`, and `contains`. - **Values**: these are the specific data points you're comparing the field identifiers against, such as a string, number, or boolean. Here's an example of a Query Predicate that filters Customers in the gold Customer Group who were last updated after a certain date. The endpoint used is `/customers`: ```bash where customerGroup(id="eb16f563-d9ca-4336-b9f3-64cce3b0123a") and lastModifiedAt>"2024-01-12T05:00:00.000Z" ``` Let’s break this Query Predicate down: - `customerGroup` is the field identifier. - `eb16f563-d9ca-4336-b9f3-64cce3b0123a` is the ID of the Customer Group we are looking for. - `and` is a logical operator that combines multiple predicates. - `lastModifiedAt` is the field identifier that references the date and time (UTC) the Customer was last updated. - `>` is the operator that checks if the date is greater than the specified value. - `2024-01-12T05:00:00.000Z` is the date. A predicate is a logical expression that evaluates to true or false and determines whether a resource is included in the query result. The example above evaluates to true. If we wanted it to evaluate to false then we can change it by wrapping it in parentheses and adding a `not` to the beginning: ```bash where not (customerGroup(id="eb16f563-d9ca-4336-b9f3-64cce3b0123a") and lastModifiedAt>"2024-01-12T05:00:00.000Z") ``` Now, this will return all Customers that belong to any Customer Group other than gold and that were last updated before `2024-01-12T05:00:00.000Z`. If we wanted to get Customers who are not assigned to a Customer Group, we would then use `customerGroup is not defined`. ## Other uses of predicates Predicates are used extensively in commercetools. Pay special attention to the syntax for each type of predicate, as they differ depending on the area of use. Here are some of the key areas where you can use predicates: - Resource querying: to filter search results when querying for Carts. - API Extensions: to specify [conditional triggers](/api/projects/api-extensions.md#conditional-triggers) for the execution of API Extensions. - Product and Cart Discounts: to define conditions for applying [Product and Cart Discounts](/api/predicates/predicate-operators.md). - Shipping Methods: to define eligibility conditions for [Shipping Methods](/api/shipping-delivery-overview.md#predicates). For detailed Query Predicate syntax and examples specific to Products and Attributes, see [Query Predicates on Attributes](/api/predicates/query.md#on-attributes) in the API documentation. ## Related pages - [Area overview page with navigation](/learning-developer-essentials.md) - [Previous page: Overview](/learning-developer-essentials/api-queries-query-predicates/overview.md) - [Next page: Queries and the SDKs](/learning-developer-essentials/api-queries-query-predicates/queries-and-the-sdks.md)