Learn More SOQL Queries.
Learn More SOQL Queries.
Q
1 ueries 1-50:Basic SOQL queries for standard and custom objects.
2. Queries 51-100:SOQL relationship queries (parent-child, child-parent).
3. Queries 101-150:SOQL semi-join and anti-join operations.
Part 1: Basic SOQL Queries for Standard and Custom Objects (1-50)
.
1
Accountrecords.
Question:Retrieve all
Answer:
.
2
Contactrecords where the
Question:Retrieve all LastNameis 'Smith'.
Answer:
.
3
Opportunitieswith an
Question:Retrieve Amountgreater than 1,000,000.
Answer:
SELECT Id, Name, Amount FROM Opportunity WHERE Amount > 1000000
.
4
Casesthat are currently
Question:Retrieve Closed
.
Answer:
.
5
Leadsthat have been converted.
Question:Retrieve
Answer:
SELECT Id, FirstName, LastName FROM Lead WHERE IsConverted = TRUE
.
6
Accountsordered by
Question:Retrieve the first 10 Name
.
Answer:
.
7
Opportunitiesthat closed in the last 30 days.
Question:Retrieve
Answer:
.
8
Contactsthat belong to
Question:Retrieve Account'Acme Inc.'.
Answer:
.
9
Tasksthat are overdue.
Question:Retrieve
Answer:
SELECT Id, Subject, ActivityDate FROM Task WHERE ActivityDate < TODAY
AND Status != 'Completed'
0.
1
Opportunitiesgrouped by
Question:Retrieve StageName
.
Answer:
SELECT Id, Name FROM Account WHERE Id NOT IN (SELECT AccountId FROM
Opportunity)
2.
1
Opportunitieswhere
Question:Retrieve Probabilityis greater than or equal to 80%.
Answer:
SELECT Id, Name, Probability FROM Opportunity WHERE Probability >= 80
3.
1
Accountsthat have a
Question:Retrieve the BillingStateof 'California'.
Answer:
4.
1
Tasksthat are associated with a particular
Question:Retrieve all Contact
.
Answer:
5.
1
Contactscreated in the last week.
Question:Retrieve all
Answer:
7.
1
Opportunitiesassociated with
Question:Retrieve all Account'Global Media'.
Answer:
8.
1
Casesthat were created in the last 24 hours.
Question:Retrieve
Answer:
9.
1
Opportunitieswith a
Question:Retrieve CloseDatein the next 7 days.
Answer:
0.
2
Accountswith a
Question:Retrieve Typeof 'Customer'.
Answer:
SELECT Id, CaseNumber, Priority FROM Case WHERE Status != 'Closed' AND
Priority = 'High'
2.
2
Opportunitieswhere the
Question:Retrieve all StageNameis 'Closed Won'.
Answer:
3.
2
Accountsthat have not been updated in the last year.
Question:Retrieve
Answer:
4.
2
Contactswhose
Question:Retrieve Titleis 'CEO'.
Answer:
5.
2
Caseswith a
Question:Retrieve all CaseOriginof 'Phone'.
Answer:
7.
2
Contactswith an
Question:Retrieve all Emaildomain of 'example.com'.
Answer:
SELECT Id, FirstName, LastName, Email FROM Contact WHERE Email LIKE
'%@example.com'
8.
2
Leadsthat have a
Question:Retrieve Ratingof 'Hot'.
Answer:
9.
2
Opportunitieswith a
Question:Retrieve Probabilityless than 20%.
Answer:
SELECT Id, Name, Probability FROM Opportunity WHERE Probability < 20
0.
3
Accountsthat have a
Question:Retrieve ShippingCountryof 'USA'.
Answer:
2.
3
Opportunitieswith a
Question:Retrieve CloseDatein the past.
Answer:
SELECT Id, Name, CloseDate FROM Opportunity WHERE CloseDate < TODAY
3.
3
Caseswhere the
Question:Retrieve Statusis 'New'.
Answer:
4.
3
Contactswith a
Question:Retrieve MailingStateof 'New York'.
Answer:
5.
3
Opportunitiesthat are in the
Question:Retrieve all Proposal/Price Quotestage.
Answer:
7.
3
Opportunitieswith a
Question:Retrieve CloseDatewithin the next 30 days.
Answer:
8.
3
Leadswith a
Question:Retrieve LeadSourceof 'Referral'.
Answer:
9.
3
Casescreated today.
Question:Retrieve
Answer:
0.
4
Contactswith a
Question:Retrieve all MailingCountryof 'Canada'.
Answer:
2.
4
Accountswith a
Question:Retrieve Typeof 'Prospect'.
Answer:
3.
4
Leadswith a
Question:Retrieve Statusof 'Open - Not Contacted'.
Answer:
SELECT Id, FirstName, LastName FROM Lead WHERE Status = 'Open - Not
Contacted'
4.
4
Opportunitiesthat are associated with
Question:Retrieve Accountsin 'California'.
Answer:
5.
4
Casesthat are
Question:Retrieve Escalated
.
Answer:
SELECT Id, Name, Probability FROM Opportunity WHERE Probability >= 50
7.
4
Accountsthat have a
Question:Retrieve BillingCityof 'New York'.
Answer:
8.
4
Contactswith a
Question:Retrieve all MailingPostalCodeof '10001'.
Answer:
9.
4
Leadsthat were created in the last 7 days.
Question:Retrieve
Answer:
0.
5
Opportunitieswith an
Question:Retrieve Amountless than 5,000.
Answer:
SELECT Id, Name, Amount FROM Opportunity WHERE Amount < 5000
2.
5
Opportunitiesrelated to
Question:Retrieve Account'Global Media'.
Answer:
3.
5
Casesrelated to
Question:Retrieve Contact'John Doe'.
Answer:
4.
5
OpportunityLineItemsrelated to
Question:Retrieve Opportunity'Opportunity1'.
Answer:
5.
5
Opportunitiesand their related
Question:Retrieve Accounts
.
Answer:
7.
5
Accountsand their related
Question:Retrieve Contacts
.
Answer:
SELECT Id, Name, (SELECT Id, FirstName, LastName FROM Contacts) FROM
Account
8.
5
Accountswith
Question:Retrieve Opportunitiesthat closed in the last month.
Answer:
SELECT Id, Name, (SELECT Id, Name FROM Opportunities WHERE CloseDate =
LAST_MONTH) FROM Account
9.
5
Contactswith their
Question:Retrieve Cases
.
Answer:
0.
6
Opportunitieswith
Question:Retrieve OpportunityLineItems
.
Answer:
2.
6
Opportunitieswith
Question:Retrieve Products
.
Answer:
3.
6
Accountswith their
Question:Retrieve Casesand
Contacts
.
Answer:
SELECT Id, Name, (SELECT Id, CaseNumber FROM Cases), (SELECT Id,
FirstName, LastName FROM Contacts) FROM Account
4.
6
Contactsand their
Question:Retrieve Opportunitiesthrough their related
Account
.
Answer:
5.
6
Opportunitiesand their
Question:Retrieve Accountand
Contactdetails.
Answer:
7.
6
Accountswith
Question:Retrieve Contactswhere the
Accountindustry is 'Technology'.
Answer:
SELECT Id, Name, (SELECT Id, FirstName, LastName FROM Contacts) FROM
Account WHERE Industry = 'Technology'
8.
6
Opportunitieswith related
Question:Retrieve Tasks
.
Answer:
SELECT Id, Name, (SELECT Id, Subject FROM Tasks) FROM Opportunity
9.
6
Contactswith their related
Question:Retrieve Caseswhere
Casestatus is 'Open'.
Answer:
0.
7
Accountswith
Question:Retrieve Opportunitiesthathave a
CloseDatein the last 30
days.
Answer:
SELECT Id, Name, (SELECT Id, Name FROM Opportunities WHERE CloseDate =
LAST_N_DAYS:30) FROM Account
1.
7
Contactsalong with their
Question:Retrieve Accountand
Caseinformation.
Answer:
2.
7
Accountswith
Question:Retrieve Opportunitiesand
Cases
.
Answer:
SELECT Id, Name, (SELECT Id, Name FROM Opportunities), (SELECT Id,
CaseNumber FROM Cases) FROM Account
3.
7
Opportunitieswith their
Question:Retrieve Accountand
Contactdetails where the
pportunityis in 'Prospecting' stage.
O
Answer:
4.
7
Contactsand their
Question:Retrieve Opportunitieswhere the
Opportunitystage is
'Closed Won'.
Answer:
5.
7
Accountswith
Question:Retrieve Contactsand
Opportunitiesthat have a
robabilityof 50% or more.
P
Answer:
SELECT Id, Name, (SELECT Id, FirstName, LastName FROM Contacts),
(SELECT Id, Name FROM Opportunities WHERE Probability >= 50) FROM
Account
6.
7
Opportunitieswith
Question:Retrieve Productswhere the
Productis 'Laptop'.
Answer:
7.
7
Question:RetrieveContactswith their
Opportunitieswhere the
Opportunityamount is
greater than 10,000.
Answer:
8.
7
Accountswith
Question:Retrieve Opportunitieswherethe
Opportunityamount is less
than 5,000.
Answer:
SELECT Id, Name, (SELECT Id, Name FROM Opportunities WHERE Amount <
5000) FROM Account
9.
7
Contactswith their
Question:Retrieve Caseswhere the
Casepriority is 'High'.
Answer:
SELECT Id, Name, (SELECT Id, FirstName, LastName FROM Contacts) FROM
Account WHERE Type = 'Customer'
1.
8
Opportunitieswith their
Question:Retrieve OpportunityLineItemswhere the
uantityis greater than 5.
Q
Answer:
2.
8
Question:Retrieve Accountswith
Opportunitieswherethe
Opportunityis in
'Qualification' stage.
Answer:
SELECT Id, Name, (SELECT Id, Name FROM Opportunities WHERE StageName =
'Qualification') FROM Account
3.
8
Contactswith their
Question:Retrieve Caseswhere the
Caseis
Closed
.
Answer:
4.
8
Opportunitieswith their
Question:Retrieve OpportunityLineItemswhere the
otalPriceis greater than 10,000.
T
Answer:
SELECT Id, Name, (SELECT Product2.Name, TotalPrice FROM
OpportunityLineItems WHERE TotalPrice > 10000) FROM Opportunity
5.
8
Accountswith
Question:Retrieve Opportunitiesthathave a
CloseDatein the next 30
days.
Answer:
SELECT Id, Name, (SELECT Id, Name FROM Opportunities WHERE CloseDate =
NEXT_N_DAYS:30) FROM Account
6.
8
Question:Retrieve Contactswith their
Opportunitieswhere the
Opportunityis in
'Negotiation' stage.
Answer:
7.
8
Opportunitieswith
Question:Retrieve OpportunityLineItemswhere the
Discountis
greater than 20%.
Answer:
8.
8
Accountswith
Question:Retrieve Opportunitieswherethe
Opportunitystage is
'Closed Lost'.
Answer:
SELECT Id, Name, (SELECT Id, Name FROM Opportunities WHERE StageName =
'Closed Lost') FROM Account
9.
8
Contactswith their
Question:Retrieve Caseswhere the
Casetype is 'Problem'.
Answer:
0.
9
Opportunitieswith
Question:Retrieve OpportunityLineItemswhere the
Quantityis
less than 10.
Answer:
1.
9
Accountswith
Question:Retrieve Contactswhere the
Contactis in 'California'.
Answer:
SELECT Id, Name, (SELECT Id, FirstName, LastName FROM Contacts WHERE
MailingState = 'California') FROM Account
2.
9
Contactswith their
Question:Retrieve Opportunitieswhere the
Opportunityamount is
between 5,000 and 10,000.
Answer:
3.
9
Accountswith
Question:Retrieve Opportunitieswherethe
Opportunitystage is
'Proposal/Price Quote'.
Answer:
SELECT Id, Name, (SELECT Id, Name FROM Opportunities WHERE StageName =
'Proposal/Price Quote') FROM Account
4.
9
Contactswith their
Question:Retrieve Caseswhere the
Caseis
Escalated
.
Answer:
5.
9
Question:Retrieve Opportunitieswith
OpportunityLineItemswhere the
Product
name is 'Software'.
Answer:
6.
9
Accountswith
Question:Retrieve Contactswhere the
Accounttype is 'Partner'.
Answer:
SELECT Id, Name, (SELECT Id, FirstName, LastName FROM Contacts) FROM
Account WHERE Type = 'Partner'
7.
9
Opportunitieswith
Question:Retrieve Productswhere the
Quantityis greater than 20.
Answer:
SELECT Id, Name, (SELECT Id, Name FROM Opportunities WHERE StageName =
'Closed Won') FROM Account
9.
9
Contactswith their
Question:Retrieve Opportunitieswhere the
Opportunityhas a
robabilityof 75% or more.
P
Answer:
00.
1
Question:Retrieve Opportunitieswith
OpportunityLineItemswhere the
TotalPrice
is less than 5,000.
Answer:
101.
Question:
Retrieve
Accounts
that have at least one
Opportunity
.
Answer:
103.
Question:
Retrieve
Opportunities
that are associated
with an
Account
from the 'Technology' industry.
Answer:
104.
Question:
Retrieve
Cases
that are related to
Contacts
who have an open
Opportunity
.
Answer:
105.
Question:
Retrieve
Accounts
that do not have any
Opportunities
.
Answer:
SELECT Id, Name FROM Account WHERE Id NOT IN (SELECT AccountId FROM
Opportunity)
106.
Question:
Retrieve
Contacts
who do not have any associated
Cases
.
Answer:
107.
Question:
Retrieve
Opportunities
that do not have
any
OpportunityLineItems
.
Answer:
108.
Question:
Retrieve
Accounts
that do not have any associated
Contacts
.
Answer:
SELECT Id, Name FROM Account WHERE Id NOT IN (SELECT AccountId FROM
Contact)
109.
Question:
Retrieve
Contacts
who do not have any associated
Opportunities
.
Answer:
SELECT Id, FirstName, LastName FROM Contact WHERE AccountId NOT IN
(SELECT AccountId FROM Opportunity)
110.
Question:
Retrieve
Opportunities
that are associated
with
Contacts
in
the 'Manufacturing' industry.
Answer:
111.
Question:
Retrieve
Cases
that do not have any associated
Tasks
.
Answer:
SELECT Id, CaseNumber FROM Case WHERE Id NOT IN (SELECT WhatId FROM
Task WHERE What.Type = 'Case')
112.
Question:
Retrieve
Accounts
that have related
Opportunities
in the
'Closed Won' stage.
Answer:
113.
Question:
Retrieve
Contacts
that are not related to
Accounts
in the
'Banking' industry.
Answer:
SELECT Id, FirstName, LastName FROM Contact WHERE AccountId NOT IN
(SELECT Id FROM Account WHERE Industry = 'Banking')
114.
Question:
Retrieve
Opportunities
that have associated
OpportunityLineItems
with a
TotalPrice
greater than 5,000.
Answer:
SELECT Id, Name FROM Opportunity WHERE Id IN (SELECT OpportunityId
FROM OpportunityLineItem WHERE TotalPrice > 5000)
115.
Question:
Retrieve
Cases
that are not associated with
Contacts
from
Accounts
in the 'Retail' industry.
Answer:
SELECT Id, CaseNumber FROM Case WHERE ContactId NOT IN (SELECT Id FROM
Contact WHERE Account.Industry = 'Retail')
116.
Question:
Retrieve
Accounts
that have no associated
Cases
.
Answer:
SELECT Id, Name FROM Account WHERE Id NOT IN (SELECT AccountId FROM
Case)
117.
Question:
Retrieve
Contacts
that are associated with
Opportunities
in
the 'Prospecting' stage.
Answer:
118.
Question:
Retrieve
Opportunities
that are not associated
with
Contacts
from the 'Financial Services' industry.
Answer:
SELECT Id, Name FROM Opportunity WHERE AccountId NOT IN (SELECT
AccountId FROM Contact WHERE Account.Industry = 'Financial Services')
119.
Question:
Retrieve
Cases
that are related to
Contacts
from
Accounts
in
the 'Healthcare' industry.
Answer:
120.
Question:
Retrieve
Accounts
that have associated
Contacts
but no
associated
Opportunities
.
Answer:
121.
Question:
Retrieve
Opportunities
that are associated
with
Accounts
having no associated
Contacts
.
Answer:
122.
Question:
Retrieve
Cases
that are not associated with
Opportunities
.
Answer:
SELECT Id, CaseNumber FROM Case WHERE Id NOT IN (SELECT WhatId FROM
Opportunity WHERE What.Type = 'Case')
123.
Question:
Retrieve
Accounts
that are associated with
Opportunities
in
the 'Qualification' stage but have no associated
Cases
.
Answer:
124.
Question:
Retrieve
Contacts
who have no associated
Cases
but are
related to
Opportunities
in the 'Closed Won' stage.
Answer:
125.
Question:
Retrieve
Opportunities
that have no associated
OpportunityLineItems
and are not in the 'Closed Lost' stage.
Answer:
126.
Question:
Retrieve
Accounts
that have no associated
Opportunities
and
no associated
Contacts
.
Answer:
SELECT Id, Name FROM Account WHERE Id NOT IN (SELECT AccountId FROM
Opportunity) AND Id NOT IN (SELECT AccountId FROM Contact)
127.
Question:
Retrieve
Contacts
who are not associated with any
Opportunities
and are from
Accounts
in the 'Nonprofit' industry.
Answer:
128.
Question:
Retrieve
Opportunities
that are associated
with
Accounts
having
Cases
in the 'Escalated' status.
Answer:
129.
Question:
Retrieve
Cases
that are related to
Opportunities
in the
'Proposal/Price Quote' stage.
Answer:
130.
Question:
Retrieve
Accounts
that have no associated
Contacts
and no
associated
Opportunities
.
Answer:
SELECT Id, Name FROM Account WHERE Id NOT IN (SELECT AccountId FROM
Contact) AND Id NOT IN (SELECT AccountId FROM Opportunity)
131.
Question:
Retrieve
Opportunities
that are associated
with
Contacts
who
have no associated
Cases
.
Answer:
132.
Question:
Retrieve
Cases
that are associated with
Opportunities
in the
'Closed Won' stage.
Answer:
133.
Question:
Retrieve
Accounts
that have associated
Opportunities
in the
'Closed Lost' stage but have no associated
Cases
.
Answer:
134.
Question:
Retrieve
Contacts
who have no associated
Opportunities
and
are related to
Accounts
in the 'Energy' industry.
Answer:
SELECT Id, FirstName, LastName FROM Contact WHERE AccountId NOT IN
(SELECT Id FROM Account WHERE Industry = 'Energy') AND AccountId NOT
IN (SELECT AccountId FROM Opportunity)
135.
Question:
Retrieve
Opportunities
that have no associated
Cases
and are
related to
Accounts
in the 'Agriculture' industry.
Answer:
SELECT Id, Name FROM Opportunity WHERE Id NOT IN (SELECT WhatId FROM
Case) AND AccountId IN (SELECT Id FROM Account WHERE Industry =
'Agriculture')
136.
Question:
Retrieve
Cases
that are associated with
Opportunities
in the
'Needs Analysis' stage.
Answer:
137.
Question:
Retrieve
Accounts
that have associated
Contacts
who have no
associated
Cases
.
Answer:
138.
Question:
Retrieve
Contacts
who are not related to
any
Opportunities
in the 'Qualification' stage.
Answer:
SELECT Id, FirstName, LastName FROM Contact WHERE AccountId NOT IN
(SELECT AccountId FROM Opportunity WHERE StageName = 'Qualification')
139.
Question:
Retrieve
Opportunities
that are associated
with
Cases
in the
'New' status.
Answer:
SELECT Id, Name FROM Opportunity WHERE Id IN (SELECT WhatId FROM Case
WHERE Status = 'New' AND What.Type = 'Opportunity')
140.
Question:
Retrieve
Accounts
that have associated
Opportunities
but no
associated
Cases
.
Answer:
141.
Question:
Retrieve
Contacts
who have no associated
Cases
but are
related to
Accounts
with associated
Opportunities
.
Answer:
142.
Question:
Retrieve
Opportunities
that are not associated
with any
Cases
but are in the 'Prospecting' stage.
Answer:
SELECT Id, Name FROM Opportunity WHERE Id NOT IN (SELECT WhatId FROM
Case) AND StageName = 'Prospecting'
143.
Question:
Retrieve
Cases
that are related to
Accounts
with no
associated
Opportunities
.
Answer:
144.
Question:
Retrieve
Contacts
who are not associated
with any
Opportunities
in the 'Proposal/Price Quote' stage.
Answer:
SELECT Id, FirstName, LastName FROM Contact WHERE AccountId NOT IN
(SELECT AccountId FROM Opportunity WHERE StageName = 'Proposal/Price
Quote')
145.
Question:
Retrieve
Opportunities
that have no associated
OpportunityLineItems
but are related to
Accounts
with
associated
Cases
.
Answer:
147.
Question:
Retrieve
Cases
that are related to
Accounts
with no
associated
Contacts
.
Answer:
148.
Question:
Retrieve
Contacts
who are not associated
with any
Cases
in
the 'Working' status.
Answer:
149.
Question:
Retrieve
Opportunities
that are related
to
Contacts
from
Accounts
with no associated
Cases
.
Answer: