Retrieve the names and phone numbers of all accounts.
SELECT Name, Phone FROM Account
Retrieve the first names and last names of all contacts who are associated with a
specific account (provide the account ID).
SELECT FirstName, LastName FROM Contact WHERE AccountId =
'001XXXXXXXXXXXXXXX'
Retrieve all opportunities that are in the 'Closed Won' stage.
SELECT Id, Name, Amount FROM Opportunity WHERE StageName = 'Closed Won'
Retrieve all accounts located in California and order them by their billing city.
SELECT Id, Name, BillingCity FROM Account WHERE BillingState = 'CA' ORDER BY
BillingCity
Retrieve the name and amount of all opportunities greater than $100,000, ordered by
amount in descending order.
SELECT Name, Amount FROM Opportunity WHERE Amount > 100000 ORDER BY Amount
DESC
Aggregate Functions and Grouping
Count the number of contacts for each account.
SELECT AccountId, COUNT(Id) FROM Contact GROUP BY AccountId
Retrieve the minimum, maximum, and average amount of opportunities grouped by their
stage name.
SELECT StageName, MIN(Amount), MAX(Amount), AVG(Amount) FROM Opportunity
GROUP BY StageName
Count the number of accounts in each industry.
SELECT Industry, COUNT(Id) FROM Account GROUP BY Industry
Relationship Queries
Retrieve all accounts with their related contacts (show account names and contact
first and last names).
SELECT Name, (SELECT FirstName, LastName FROM Contacts) FROM Account
Retrieve all contacts along with their parent account names.
SELECT FirstName, LastName, Account.Name FROM Contact
Advanced Filtering
Retrieve all leads created in the last 7 days.
SELECT Id, FirstName, LastName, CreatedDate FROM Lead WHERE CreatedDate =
LAST_N_DAYS:7
Retrieve all accounts in the technology industry with an annual revenue greater
than $1,000,000.
SELECT Id, Name, AnnualRevenue FROM Account WHERE Industry = 'Technology' AND
AnnualRevenue > 1000000
Retrieve all cases with a priority of 'High' and created in the last 30 days.
SELECT Id, CaseNumber, Priority, CreatedDate FROM Case WHERE Priority =
'High' AND CreatedDate = LAST_N_DAYS:30
Pagination
Retrieve the first 5 accounts ordered by their name.
SELECT Id, Name FROM Account ORDER BY Name ASC LIMIT 5
Retrieve the next set of 5 accounts using OFFSET (assume you've already retrieved
the first 5).
SELECT Id, Name FROM Account ORDER BY Name ASC LIMIT 5 OFFSET 5
Duplicate Records
Find duplicate contacts by email address.
SELECT Email, COUNT(Id) FROM Contact GROUP BY Email HAVING COUNT(Id) > 1
Find duplicate accounts by name.
SELECT Name, COUNT(Id) FROM Account GROUP BY Name HAVING COUNT(Id) > 1
Using Subqueries
Retrieve accounts with more than one related contact.
SELECT Name, (SELECT COUNT(Id) FROM Contacts) ContactCount FROM Account
HAVING (SELECT COUNT(Id) FROM Contacts) > 1
Retrieve opportunities with their related account names and owner names.
SELECT Name, Account.Name, Owner.Name FROM Opportunity
Retrieve accounts along with the number of contacts related to each account.
SELECT Name, (SELECT COUNT(Id) FROM Contacts) FROM Account
Complex Queries
Retrieve accounts with specific fields (e.g., name, industry) and related contact
emails for contacts located in New York.
SELECT Name, Industry, (SELECT Email FROM Contacts WHERE MailingState = 'NY')
FROM Account
Retrieve cases with their related contact and account information, where the cases
were created in the last 60 days.
SELECT Id, CaseNumber, Contact.FirstName, Contact.LastName, Account.Name FROM
Case WHERE CreatedDate = LAST_N_DAYS:60
Retrieve all opportunities along with their account names and the names of their
related primary contacts.
SELECT Name, Account.Name, (SELECT Contact.FirstName, Contact.LastName FROM
OpportunityContactRoles WHERE IsPrimary = TRUE) FROM Opportunity
write soql in top 10 account record based on amount
SELECT Id, Name, Total_Sales__c FROM Account ORDER BY Total_Sales__c DESC
LIMIT 10
write soql in account is not present contact
SELECT Id, Name FROM Account WHERE Id NOT IN (SELECT AccountId FROM Contact)
fetch all accounts which have more than 3contacts
Select id,(Select Count(id) from contact) from Account having (Select
Count(id) from contact) > 3
Select id,name from account where id IN (Select id,AccountID From
contact Group by AccountID having Count(Id)>3)
Select AccountId,Count(id) from Contact GROUP BY AccountId Having Count(id) <
3
Write soql query to fetch last month records
Select id,name,CreatedDate from Account where CreatedDate = Last_N_Months:1
To fetch account two column id and contact count which has more than one contact
related to it
Select id,(Select Count(id) from Contact) ContactCount from Account Having
(Select Count(id) from Contact) > 1
To fetch account related contact
Select id,name,(Select id,name from Contact) from Account
Soql query opportunity to start name with A
Select id,Name,StageName,Account.Name from Opportunity where Name Like 'a%'
Soql query to fetch the opportunity related account name
Select id,Name,StageName,Account.Name from Opportunity
write a soql query to fetch account related opportunity count and show less than 10
Select AccountId,Count(id) Opportunitycount from Opportunity GROUP BY
AccountId Having Count(id) < 10
Write soql query to fetch account record with two specific date between
SELECT Id, Name, CreatedDate FROM Account WHERE CreatedDate >= 2024-01-
01T00:00:00Z AND CreatedDate <= 2024-01-31T23:59:59Z
Retrieve all Accounts that have had more than three Opportunities closed in the
past year. Include the Account Name,the number of closed Opportunities,
and the sum of their Amounts.
SELECT AccountId, Account.Name,COUNT(Id) AS
ClosedOpportunityCount,SUM(Amount) AS TotalAmount FROM Opportunity WHERE IsClosed =
TRUE
AND CloseDate = LAST_N_DAYS:365 GROUP BY AccountId,Account.Name HAVING
COUNT(Id) > 3
SELECT Name,(SELECT COUNT(Id) AS ClosedOpportunityCount,SUM(Amount) AS
TotalAmount FROM Opportunities WHERE IsClosed = TRUE AND CloseDate =
LAST_N_DAYS:365)
FROM Account WHERE Id IN (SELECT AccountId FROM Opportunity WHERE IsClosed =
TRUE AND CloseDate = LAST_N_DAYS:365 GROUP BY AccountId HAVING COUNT(Id) > 3)
thse user to sell the product owner of the opportunity record
regin is on user record
find top 3 regin the defination highest amound the top regin stagename closed won
SELECT Region__c, SUM(Amount) totalAmount FROM Opportunity WHERE StageName =
'Closed Won' GROUP BY Region__c ORDER BY totalAmount DESC LIMIT 3