0% found this document useful (0 votes)
59 views10 pages

Soql and Sosl

The document provides an overview of SOQL (Salesforce Object Query Language), including its syntax, relationship queries, limits, and key features. It also covers the use of aggregate functions, dynamic queries, and the differences between WHERE and HAVING clauses. Additionally, it discusses SOSL for searching across multiple objects and provides various examples of SOQL queries.

Uploaded by

Sushant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views10 pages

Soql and Sosl

The document provides an overview of SOQL (Salesforce Object Query Language), including its syntax, relationship queries, limits, and key features. It also covers the use of aggregate functions, dynamic queries, and the differences between WHERE and HAVING clauses. Additionally, it discusses SOSL for searching across multiple objects and provides various examples of SOQL queries.

Uploaded by

Sushant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Interview Questions and Answers

1. What is SOQL?
SOQL (Salesforce Object Query Language) is a query language used to search Salesforce data for
specific information. It is similar to SQL (Structured Query Language) but is designed specifically
for Salesforce’s object-oriented database. SOQL is used to retrieve records from Salesforce
objects and their related objects.
____________________________________________________________________________

2. Can you explain the basic syntax of a SOQL query?


The basic syntax of a SOQL query is:
SELECT field1, field2 FROM object WHERE condition
SELECT is used to specify the fields you want to retrieve.
FROM specifies the object from which you want to retrieve data.
WHERE specifies the filter criteria.
Example:
SELECT Name, Email FROM Contact WHERE Account.Name = ‘Edge Communication’
______________________________________________________________________________

3. What are relationship queries in SOQL?


Relationship queries in SOQL allow you to query related objects in Salesforce. Salesforce has
two types of relationships:
Parent-to-Child (Inner Query): This allows you to query related child records.
Child-to-Parent (Outer Query): This allows you to access parent object fields from a child object.
Example (Parent-to-Child):
SELECT Name, (SELECT LastName FROM Contacts) FROM Account
Example (Child-to-Parent):
SELECT LastName, Account.Name FROM Contact
______________________________________________________________________________
4. What are some limits in SOQL?
Salesforce imposes several limits on SOQL queries to ensure efficient performance. Some of
these limits are:
• Total number of records returned: 50,000 records per query.
• Total query execution time: 10 seconds for synchronous queries.
• Maximum number of records retrieved in a batch: 2000 records in a batch Apex.
• SOQL query rows per transaction: 100,000 rows per transaction.
______________________________________________________________________________

5.What is the purpose of using GROUP BY in SOQL?


GROUP BY in SOQL is used to group records based on one or more fields, often in combination
with aggregate functions (like COUNT, SUM, MAX, etc.). It helps to get summary data.
Example:
SELECT COUNT(Id), AccountId FROM Contact GROUP BY AccountId
This query will return the number of contacts grouped by each Account.
______________________________________________________________________________

6. What are aggregate functions in SOQL?


Aggregate functions in SOQL allow you to perform calculations on the data returned. Common
aggregate functions include:
• COUNT(): Returns the number of records.
• SUM(): Returns the sum of a numeric field.
• MAX(): Returns the maximum value of a field.
• MIN(): Returns the minimum value of a field.
• AVG(): Returns the average value of a field.
Example:
SELECT AVG(Amount) FROM Opportunity WHERE StageName = 'Closed Won'
______________________________________________________________________________
7. How do you handle large datasets in SOQL?
When querying large datasets, it is important to consider best practices for performance:
• Use LIMIT to restrict the number of records returned.
• Use indexed fields in the WHERE clause to speed up queries.
• Avoid SELECT * (SELECT all fields), instead, specify only the necessary fields.
• Pagination can be used to break the query results into smaller chunks, especially when
querying large datasets.
______________________________________________________________________________

8. What is a dynamic SOQL query?


A dynamic SOQL query is a query where the SOQL string is built dynamically at runtime. This can
be useful when the query needs to be adjusted based on input or conditions. For example, using
Apex code:
String query = 'SELECT Name FROM Account WHERE Name = :accountName';
List<Account> results = Database.query(query);
______________________________________________________________________________

9. What is the difference between WHERE and HAVING in SOQL?


• WHERE is used to filter rows before any grouping happens.
• HAVING is used to filter groups after GROUP BY has been applied. However, in SOQL, the
HAVING clause is not supported, so filtering after grouping must be done in the WHERE
clause or using a workaround.
______________________________________________________________________________

10. Can you explain how to query records using IN in SOQL?


You can use the IN operator in SOQL to filter records based on a list of values. For example:
SELECT Name FROM Account WHERE Industry IN ('Technology', 'Finance')
This will retrieve all Accounts with either 'Technology' or 'Finance' as the Industry.
______________________________________________________________________________
11. How do you query records with null values in SOQL?
To query records with null values in SOQL, you can use the NULL keyword in the WHERE clause.
For example:
SELECT Name FROM Contact WHERE Email = NULL
This will retrieve all contacts where the Email field is empty.

12. What are the key features of SOQL?


SELECT Clause: Allows selecting specific fields or all fields (using *).
FROM Clause: Defines the object you are querying.
WHERE Clause: Adds filters or conditions to the query.
ORDER BY Clause: Allows sorting the results.
LIMIT Clause: Restricts the number of records returned.
GROUP BY Clause: Used to group records by fields and perform aggregate functions like COUNT,
SUM, AVG, etc.
Relationship Queries: Supports parent-to-child and child-to-parent relationships.
Aggregate Functions: Supports functions like COUNT(), MAX(), MIN(), SUM(), AVG().

13. What is the purpose of WITH SECURITY_ENFORCED in SOQL?


The WITH SECURITY_ENFORCED clause ensures that the query respects the field-level security
settings and object permissions. When this clause is included, the query will only return records
that the user has access to, ensuring that users can only see data they have permission to view
based on their profile or permission sets.
Example:
SELECT Name, AnnualRevenue FROM Account WITH SECURITY_ENFORCED
14. What is the use of IN and NOT IN in SOQL?
The IN and NOT IN operators are used to match values from a list of values in SOQL.

IN is used to filter records where a field matches any value in a list.

Example:

SELECT Name FROM Account WHERE Industry IN ('Technology', 'Healthcare')

NOT IN is used to filter records where a field does not match any value in the list.

Example:

SELECT Name FROM Account WHERE Industry NOT IN ('Technology', 'Healthcare')

______________________________________________________________________________

15. What is the purpose of SOSL and when would you use it?
SOSL is used to search across multiple objects in Salesforce for text or keyword-based searches.
It is especially useful when you want to search records based on user input, and you don’t know
which object contains the record. SOSL returns results from multiple objects (standard and
custom), such as Accounts, Contacts, Leads, etc.
You would use SOSL when:
You need to perform a broad search across multiple objects.
You need to search for records by keywords in text fields.
You are performing full-text searches across various fields and objects.
______________________________________________________________________________

16. How do you perform a search using SOSL?


SOSL performs a search using the FIND keyword followed by the search term. Here’s an example
of a simple SOSL query that searches for a keyword in multiple objects:
FIND {Acme}
IN ALL FIELDS
RETURNING Account(Name), Contact(FirstName, LastName), Opportunity(Name)
This query searches for the term "Acme" in all text fields across the Account, Contact, and
Opportunity objects. The RETURNING clause specifies the fields to return from the matched
objects.
17. What are some limitations of SOSL?
• Field Type Limitations: SOSL only searches text fields, such as Name, Email, and other
text-based fields. It cannot search in non-text fields like number or date fields.
• Search Term Length: The search term in SOSL must be at least 3 characters long.
• Return Limitations: SOSL queries are limited to returning a maximum of 2000 records
per object type by default.

EXAMPLES:
18. Query with Relationship (Parent-to-Child)
This query retrieves all Contacts related to a specific Account, including the Account Name.

SELECT Name, (SELECT FirstName, LastName FROM Contacts)

FROM Account

WHERE Name = 'Acme Corporation'

The query selects the Account Name and the Contacts related to the Account.

19. Query with Relationship (Child-to-Parent)


This query retrieves all Contacts and includes the related Account information.

SELECT FirstName, LastName, Account.Name

FROM Contact

WHERE Account.Name = 'Acme Corporation'

The query selects Contact details and includes Account details, specifically the Account Name.

20. Using ORDER BY with LIMIT


This query retrieves the latest 5 Opportunities ordered by the Close Date.

SELECT Name, CloseDate

FROM Opportunity

WHERE StageName = 'Closed Won'

ORDER BY CloseDate DESC

LIMIT 5

The query retrieves the most recent closed-won Opportunities sorted by the Close Date.
21. Query with Multiple Filters (AND/OR)
This query fetches Accounts in the 'Technology' or 'Healthcare' industry, with a revenue greater
than $10 million.

SELECT Name, Industry, AnnualRevenue

FROM Account

WHERE (Industry = 'Technology' OR Industry = 'Healthcare')

AND AnnualRevenue > 10000000

The query filters Accounts by industry and annual revenue.

22.Using IN for Multiple Values


This query retrieves Contacts that belong to multiple Accounts.

SELECT FirstName, LastName, Account.Name

FROM Contact

WHERE Account.Name IN ('Acme Corporation', 'Global Tech')

The query filters Contacts belonging to the specified Accounts.

23. Query with Date Ranges


This query retrieves all Opportunities created in the last 30 days.

SELECT Name, CreatedDate

FROM Opportunity

WHERE CreatedDate >= LAST_N_DAYS:30

The query retrieves Opportunities created in the last 30 days.

24. Subquery with Aggregate Functions


Retrieve Accounts and the total amount of opportunities for each Account.

SELECT Name, (SELECT SUM(Amount) FROM Opportunities)

FROM Account

GROUP BY Name

The query aggregates the Amount from Opportunities related to each Account.
25. Query with LIKE and Wildcards
This query retrieves Accounts whose names contain the word "Global".

SELECT Name

FROM Account

WHERE Name LIKE '%Global%'

The query retrieves Accounts with names that contain "Global."

26. Query with NOT IN


Retrieve Contacts who are not related to a specific set of Accounts.

SELECT FirstName, LastName, Account.Name

FROM Contact

WHERE Account.Name NOT IN ('Acme Corporation', 'Global Tech')

The query retrieves Contacts not related to the specified Accounts.

______________________________________________________________________________

27. Using WITH Security Predicate


This query retrieves all Opportunities, ensuring that only those the user has access to are
returned.

SELECT Name, Amount, StageName

FROM Opportunity

WITH SECURITY_ENFORCED

You might also like