Filter Your Query Results

A query can return many rows, depending on the amount of data stored in Data 360. You can filter the retrieved rows by using a WHERE clause.

This example shows a field expression in the WHERE clause containing one field. It filters the result set to return only one Individual record with the specified ID value.

This query returns exactly one record that matches the specified ID.

ssot__FirstName__cssot__LastName__cssot__Salutation__c
EdnaFrankMs.

The previous query filters on the ID field. You can add more than one filter condition in the WHERE clause by combining them with the AND and OR logical operators. For example, instead of retrieving only one record, you can retrieve all individuals whose last name is either Frank or Green and whose birth date field isn’t null.

This query output shows sample results.

ssot__Id__cssot__FirstName__cssot__LastName__cssot__BirthDate__c
003Hu00003SELnIIAXAviGreen1937-03-12T00:00:00.000Z
003Hu00003SELnNIAXEdnaFrank1945-10-23T00:00:00.000Z

Another possible scenario is that you want to retrieve all the Generation X individuals with birth dates from 1965 through 1980. To specify a date range to filter on, the WHERE clause uses the BETWEEN operator. The BETWEEN operator compares the birth date against a range defined by two values and includes the beginning and ending values. In addition to the BETWEEN operator, this query uses the TIMESTAMP operator to convert the string values to timestamps so that they can be compared.

This query output shows sample results.

ssot__Id__cssot__FirstName__cssot__LastName__cssot__BirthDate__c
003Hu00003SELn6IAHRoseGonzalez1971-08-13T00:00:00.000Z
003Hu00003SELnMIAXLizD'Cruz1965-05-18T00:00:00.000Z

Sometimes, you want to search for records based on some character patterns. For example, you want to get all individuals whose first name starts with the letter ‘E’. Use the LIKE operator to compare patterns in string values. Pattern matching is case-sensitive. You can use these symbols in the string pattern.

  • An underscore (_) stands for any single character.
  • A percent sign (%) matches any sequence of zero or more characters.

This query returns all individuals whose first name starts with ‘E’. For example, a sample result can include individuals with the first names of Eugena and Edna.

This query output shows sample results. Eugena has no birth date value, so the ssot__BirthDate__c column for Eugena is empty.

ssot__Id__cssot__FirstName__cssot__LastName__cssot__BirthDate__c
003Hu00003SELnNIAXEdnaFrank1945-10-23T00:00:00.000Z
00QHu00003W7JISMA3EugenaLuce

This query uses two underscores in the LIKE pattern. It finds all individuals whose first name starts with ‘J’, ends with ‘e’, and is four characters long. A sample result can include individuals with the first names of Jake and Jane.

This query output shows sample results. The two matching records are for Jake and Jane. Jake has no birthdate value, so the ssot__BirthDate__c column for Jake is empty.

ssot__Id__cssot__FirstName__cssot__LastName__cssot__BirthDate__c
003Hu00003SELnPIAXJakeLlorrac
003Hu00003SELnGIAXJaneGrey1947-10-01T00:00:00.000Z

Besides LIKE and BETWEEN operators, SQL also supports the commonly known comparison operators:

  • >
  • >=
  • <
  • <=
  • =
  • <>, not equals
  • !=, an alternative syntax for <>

A secondary index speeds up filters and joins on a non-primary-key field and can reduce credit usage.

To filter with a secondary index, refer to the indexed field directly. Equality (=) and range comparisons (>, >=, <, <=, and BETWEEN) on the indexed field use the index. The engine can’t use the index when the indexed field is wrapped in a function or calculation, or matched with a LIKE pattern.

This filter uses the index because it’s a direct match on the indexed field.

When a function hides the indexed field, rewrite the condition to compare the field directly. For example, instead of filtering on a birth year with YEAR(), use a range on the field itself.

When you filter on an indexed field along with other fields, combine the conditions with AND. The secondary index narrows the result set first, and then the remaining conditions are evaluated against the smaller set.

When you join on a non-primary field, filtering the indexed side to significantly fewer rows than the other side gives the best performance.

For secondary index creation, supported object types, and limitations, see Optimize Queries with Secondary Indexes in Salesforce Help.