PD1 SOQL and SOSL 2 — Questions and Answers
Question 1: What is the primary functional difference between SOQL and SOSL?
- SOQL searches text fields only, while SOSL queries all field types
- SOSL can search across multiple objects simultaneously, while SOQL queries one object at a time (Correct answer)
- SOQL is used in Apex while SOSL is used only in Visualforce
- SOSL supports aggregate functions while SOQL does not
Correct answer: SOSL can search across multiple objects simultaneously, while SOQL queries one object at a time
SOSL performs text searches across multiple Salesforce objects simultaneously, whereas SOQL queries a single object (with optional subqueries for related objects).
Question 2: What is the maximum number of records SOSL returns per object in search results?
- 100
- 200 (Correct answer)
- 500
- 2000
Correct answer: 200
SOSL returns a maximum of 200 records per object included in the search results.
Question 3: Which SOQL clause is used with aggregate functions to group query results by a specified field?
- HAVING
- GROUP BY (Correct answer)
- AGGREGATE BY
- PARTITION BY
Correct answer: GROUP BY
The GROUP BY clause in SOQL groups query results by specified fields when used alongside aggregate functions like COUNT() or SUM().
Question 4: What is a bind variable in the context of a SOQL query in Apex?
- A variable that links two related objects together
- An Apex variable referenced in a SOQL query using a colon (:) prefix (Correct answer)
- A query parameter passed from a Visualforce page controller
- A constant value hardcoded in a SOQL WHERE clause
Correct answer: An Apex variable referenced in a SOQL query using a colon (:) prefix
A bind variable is an Apex variable prefixed with a colon in a SOQL query (e.g., WHERE Name = :myVar), safely injecting the value without risk of SOQL injection.
Question 5: Which SOQL clause filters aggregate query results AFTER the GROUP BY operation has been applied?
- WHERE
- FILTER
- HAVING (Correct answer)
- RESTRICT
Correct answer: HAVING
The HAVING clause filters grouped/aggregated results after GROUP BY is applied, unlike WHERE which filters individual records before grouping.
Question 6: What is the effect of adding a FOR UPDATE clause to a SOQL query in Apex?
- It marks records as pending for a future DML update
- It locks the returned records to prevent concurrent modification by other transactions (Correct answer)
- It returns only records modified during the current transaction
- It automatically updates the returned records using current field values
Correct answer: It locks the returned records to prevent concurrent modification by other transactions
FOR UPDATE locks the retrieved records so that other transactions cannot modify them simultaneously, enabling safe concurrent processing.
Question 7: In which scenario is SOSL preferred over SOQL?
- When querying a single object using a known record ID
- When searching for a text string across multiple objects and fields simultaneously (Correct answer)
- When performing aggregate calculations on numeric fields
- When you need to return more than 50,000 records in one call
Correct answer: When searching for a text string across multiple objects and fields simultaneously
SOSL is the better choice when you need to find a text value across multiple objects and fields at once, since SOQL can only query one object per statement.
What is the primary functional difference between SOQL and SOSL?