0% found this document useful (0 votes)
58 views

SOQL Query

Uploaded by

subakirudhu
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)
58 views

SOQL Query

Uploaded by

subakirudhu
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/ 5

1

SOQL Queries

Q: What is SOQL Query?


A: SOQL (Salesforce Object Query Language) is a query language used to search and retrieve records from
Salesforce objects. It is similar to SQL (Structured Query Language) and is specifically designed for
querying data within the Salesforce platform.

Q: Can you explain the difference between SOQL and SOSL?


A: SOQL (Salesforce Object Query Language) is used to search and retrieve records from a single
Salesforce object at a time. It is similar to SQL and allows you to perform queries like SELECT, UPDATE,
and DELETE on individual objects.

On the other hand, SOSL (Salesforce Object Search Language) is used to perform text-based searches across
multiple objects simultaneously. It allows you to search for a specific term or phrase across different object
types and returns a list of records that match the search criteria.

Q: How can you filter records based on a specific condition in SOQL?


A: You can filter records based on a specific condition in SOQL by using the WHERE clause in your query.
The WHERE clause allows you to specify the conditions that must be met for a record to be returned in the
query results. For example, the query "SELECT Name FROM Account WHERE Industry = 'Technology'"
will retrieve the names of all accounts in the Technology industry.

Q: How can you retrieve related records using SOQL relationships?


A: You can retrieve related records using SOQL relationships by using the relationship fields in your query.
Salesforce objects have different types of relationships, such as lookup relationships and master-detail
relationships, which allow you to link records together.

To retrieve related records, you can use the dot notation in your query. For example, if you have a custom
object "Invoice c" that has a lookup relationship with the "Account" object, you can retrieve the account
name for each invoice using the query "SELECT Name, Account r.Name FROM Invoice c".

Q: How can you limit the number of records returned by a SOQL query?
A: You can limit the number of records returned by a SOQL query by using the LIMIT keyword. The
LIMIT keyword allows you to specify the maximum number of records to be returned in the query results.
For example, the query "SELECT Name FROM Account LIMIT 10" will return only the first 10 account
names.

Q: How can you sort the query results in a specific order using SOQL?
A: You can sort the query results in a specific order using the ORDER BY clause in SOQL. The ORDER
BY clause allows you to specify the field(s) by which you want to sort the results and the sort direction
(ascending or descending). For example, the query "SELECT Name FROM Account ORDER BY Name
DESC" will return the account names in descending alphabetical order.

Q: Can you explain the difference between a SOQL query and a subquery?
A: A SOQL query is a standalone query that retrieves data from a single Salesforce object. It can be used to
search, filter, and sort records within that object.

On the other hand, a subquery is a query that is nested inside another query. It allows you to retrieve data
from related objects in a single query. The results of the subquery are used as a filter condition or to retrieve
additional information about the records returned by the outer query.
Sweta Prajapati – Senior Salesforce Developer
User | HP
2
SOQL Queries

Q: How can you perform aggregate functions in SOQL?


A: You can perform aggregate functions in SOQL to calculate and summarize data. SOQL supports several
aggregate functions such as COUNT, SUM, AVG, MAX, and MIN. These functions allow you to count
records, calculate the sum or average of a field, or find the maximum or minimum value of a field.

SCENARIOS

1) Retrieve all Account records.


SOQL Query: SELECT Name, Industry, BillingCity, BillingState FROM Account

2) Retrieve Contact records associated with an Account.


SOQL Query: SELECT Name, Email, Phone FROM Contact WHERE AccountId =
'001XXXXXXXXXXXX'

3) Retrieve Opportunities with an amount greater than $10,000.


SOQL Query: SELECT Name, StageName, Amount FROM Opportunity WHERE Amount >
10000

4) Retrieve Case records created in the last 30 days.


SOQL Query: SELECT CaseNumber, Subject, Status FROM Case WHERE CreatedDate =
LAST_N_DAYS:30

5) Retrieve all Lead records converted to an Opportunity.


SOQL Query: SELECT Name, ConvertedAccountId, ConvertedOpportunityId FROM Lead
WHERE IsConverted = TRUE

6) Retrieve Product records with a specific Product Family.


SOQL Query: SELECT Name, ProductCode, IsActive FROM Product2 WHERE ProductFamily =
'Electronics'

7) Retrieve Task records related to a specific User.


SOQL Query: SELECT Subject, Status, Priority FROM Task WHERE OwnerId =
'005XXXXXXXXXXXX'

8) Retrieve Custom Object records sorted by a custom field in ascending order.


SOQL Query: SELECT Name, CustomField c FROM Custom_Object c ORDER BY
CustomField c ASC

9) Retrieve Account records with related Opportunities.


SOQL Query: SELECT Name, (SELECT Name, StageName, Amount FROM Opportunities)
FROM Account

10) Retrieve Campaign records with the number of associated Leads.


SOQL Query: SELECT Name, (SELECT Id FROM Leads) FROM Campaign

11) Retrieve all account names and their associated contacts' email addresses.
SOQL Query: SELECT Name, (SELECT Email FROM Contacts) FROM Account

12) Retrieve all opportunities with a close date in the current quarter.

Sweta Prajapati – Senior Salesforce Developer


User | HP
3
SOQL Queries
SOQL Query: SELECT Name, CloseDate FROM Opportunity WHERE CloseDate =
THIS_QUARTER

13) Retrieve all cases owned by a specific user and their associated account names.
SOQL Query: SELECT CaseNumber, Account.Name FROM Case WHERE OwnerId =
'005XXXXXXXXXXXX'

14) Retrieve all leads created in the last 30 days.


SOQL Query: SELECT FirstName, LastName, CreatedDate FROM Lead WHERE CreatedDate =
LAST_N_DAYS:30

15) Retrieve all custom objects named "Project" with a status of "In Progress".
SOQL Query: SELECT Name, Status c FROM Project c WHERE Status c = 'In Progress'

16) Retrieve all contacts related to an account with a specific account ID.
SOQL Query: SELECT FirstName, LastName, Email FROM Contact WHERE AccountId =
'001XXXXXXXXXXXX'

17) Retrieve the total amount of closed-won opportunities for each account.
SOQL Query: SELECT Account.Name, SUM(Amount) FROM Opportunity WHERE StageName =
'Closed Won' GROUP BY Account.Name

18) Retrieve all products with a quantity in stock less than 10.
SOQL Query: SELECT Name, Quantity c FROM Product c WHERE Quantity c < 10

19) Retrieve the top 5 opportunities by amount in descending order.


SOQL Query: SELECT Name, Amount FROM Opportunity ORDER BY Amount DESC LIMIT 5

20) Retrieve all tasks that are overdue and assigned to the current user.
SOQL Query: SELECT Subject, ActivityDate FROM Task WHERE IsClosed = FALSE AND
ActivityDate < TODAY AND OwnerId = '005XXXXXXXXXXXX'

21) You are a Salesforce administrator and need to retrieve the names and email addresses of all contacts
in the system.
SOQL Query: SELECT Name, Email FROM Contact.

22) You are a sales manager and want to find all opportunities with a close date in the next 30 days.
SOQL Query: SELECT Name, CloseDate FROM Opportunity WHERE CloseDate >=
NEXT_N_DAYS:0 AND CloseDate <= NEXT_N_DAYS:30

23) As a support agent, you want to retrieve the cases created in the last week for a specific account.
SOQL Query: SELECT CaseNumber, Subject, CreatedDate FROM Case WHERE AccountId =
'001XXXXXXXXXXXX' AND CreatedDate = LAST_WEEK

24) You are a marketing analyst and need to calculate the total number of leads by source.
SOQL Query: SELECT LeadSource, COUNT(Id) FROM Lead GROUP BY LeadSource

25) As a service manager, you want to retrieve the cases with their associated accounts and the contact
names for each case.
SOQL Query: SELECT CaseNumber, Account.Name, Contact.Name FROM Case

Sweta Prajapati – Senior Salesforce Developer


User | HP
4
SOQL Queries
26) You are a developer and need to retrieve the opportunities with a stage of "Closed Won" and an
amount greater than $100,000.
SOQL Query: SELECT Name, Amount FROM Opportunity WHERE StageName = 'Closed Won'
AND Amount > 100000

27) As a human resources manager, you want to find all employees whose job title contains the word
"Manager".
SOQL Query: SELECT Name, Job_Title c FROM Employee c WHERE Job_Title c LIKE
'%Manager%'

28) You are a system administrator and want to retrieve all accounts along with their related contacts.
SOQL Query: SELECT Name, (SELECT FirstName, LastName FROM Contacts) FROM Account

29) As a support agent, you need to retrieve the cases with their associated contacts and the account
name for each case, where the case status is "Escalated".
SOQL Query: SELECT CaseNumber, Contact.Name, Account.Name FROM Case WHERE Status
= 'Escalated'

30) You are a sales representative and want to find all opportunities owned by your user ID.
SOQL Query: SELECT Name, StageName FROM Opportunity WHERE OwnerId =
'005XXXXXXXXXXXX'

31) You want to retrieve the names and email addresses of all contacts in the "IT" department.
SOQL Query: SELECT Name, Email FROM Contact WHERE Department c = 'IT'

32) You need to retrieve the account names and their associated opportunities that have a close date
within the current month.
SOQL Query: SELECT Name, (SELECT Name, CloseDate FROM Opportunities WHERE
CloseDate = THIS_MONTH) FROM Account

33) You want to find all cases that are open and have a priority of "High" or "Critical".
SOQL Query: SELECT CaseNumber, Subject, Priority FROM Case WHERE Status = 'Open' AND
Priority IN ('High', 'Critical')

34) You need to retrieve the names and contact phone numbers of all accounts that have at least one
opportunity with a stage of "Closed Won".
SOQL Query: SELECT Name, Phone, (SELECT Name, StageName FROM Opportunities WHERE
StageName = 'Closed Won') FROM Account

35) You want to find all leads that were created in the last 7 days and are not yet converted.
SOQL Query: SELECT FirstName, LastName, CreatedDate FROM Lead WHERE IsConverted =
FALSE AND CreatedDate = LAST_N_DAYS:7

36) You need to retrieve the names and email addresses of all contacts associated with accounts in the
"Technology" industry.
SOQL Query: SELECT Name, Email FROM Contact WHERE Account.Industry = 'Technology'

37) You want to find all custom objects named "Project c" that have a start date greater than today.
SOQL Query: SELECT Name, Start_Date c FROM Project c WHERE Start_Date c >
TODAY.

Sweta Prajapati – Senior Salesforce Developer


User | HP
5
SOQL Queries
38) Retrieve the names and email addresses of all Contact records where the Account Industry is
'Finance'.
SOQL Query: SELECT Name, Email FROM Contact WHERE Account.Industry = 'Finance'

39) Retrieve the Opportunity records with a Close Date greater than today and an Amount greater than
$10,000.
SOQL Query: SELECT Name, CloseDate, Amount FROM Opportunity WHERE CloseDate >
TODAY AND Amount > 10000

40) Retrieve the names and phone numbers of all Lead records created in the last 7 days.
SOQL Query: SELECT Name, Phone FROM Lead WHERE CreatedDate = LAST_N_DAYS:7

41) Retrieve the Account records sorted by the Account Name in ascending order.
SOQL Query: SELECT Name, Industry, BillingCity FROM Account ORDER BY Name ASC

42) Retrieve the Case records with a Status of 'Open' and the Case Origin as either 'Email' or 'Phone'.
SOQL Query: SELECT CaseNumber, Status, Origin FROM Case WHERE Status = 'Open' AND
(Origin = 'Email' OR Origin = 'Phone')

43) Retrieve the Opportunity records and their associated Account names.
SOQL Query: SELECT Name, Account.Name FROM Opportunity

44) Retrieve the names and email addresses of all Contact records where the Account Industry is
'Technology' and the Contact has a Mailing City of 'San Francisco'.
SOQL Query: SELECT Name, Email FROM Contact WHERE Account.Industry = 'Technology'
AND MailingCity = 'San Francisco'

45) Retrieve the names and created dates of the three most recent Case records.
SOQL Query: SELECT Subject, CreatedDate FROM Case ORDER BY CreatedDate DESC LIMIT
3

46) The names and total amounts of all Opportunities grouped by their Stage.
SOQL Query: SELECT StageName, SUM(Amount) FROM Opportunity GROUP BY StageName

Sweta Prajapati – Senior Salesforce Developer


User | HP

You might also like