SOQL Interview questions - Part 1

 SOQL Intermediate level to Advanced level - Part 1

Here are some simple to tricky interview questions asked on SOQL. if you are new to Salesforce development or SOQL, before going through below list of questions, please refer the provided link which explains about SOQL basic syntax and instructions on how to construct the query.

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql.htm

1: Accounts with no contacts


SELECT Name FROM Account WHERE Id NOT IN (SELECT AccountId FROM Contact)


2: Contacts without Opportunities through Account


SELECT Id,LastName FROM Contact WHERE AccountId NOT IN (SELECT AccountId FROM Opportunity)


3. Fetch Accounts created in the last 30 days that have at least one related Opportunity.


SELECT Name FROM Account WHERE CreatedDate = LAST_N_DAYS:30 AND Id IN (SELECT AccountId FROM Opportunity)


4: Find Contacts that share the same Email address (show Name and Email).


SELECT Email, COUNT(Id) FROM Contact WHERE Email != null GROUP BY Email HAVING COUNT(Id) > 1


5: Fetch top 3 Accounts having the highest count of related Opportunities.


SELECT AccountId, COUNT(Id) FROM Opportunity GROUP BY AccountId  ORDER BY COUNT(Id) DESC LIMIT 3


6: Display all Opportunities closing in the current month where Account is active.


SELECT Name FROM Opportunity WHERE CloseDate = THIS_MONTH AND AccountId IN (SELECT Id FROM Account WHERE Active__c = ‘TRUE’)


7: Fetch all Accounts that have at least one Contact and at least one Opportunity.


SELECT Name FROM Account WHERE Id IN (SELECT AccountId FROM Contact) AND Id IN (SELECT AccountId FROM Opportunity)

8:Write a query to return Account names with more than 5 related Contacts

SELECT AccountId, COUNT(Id) FROM Contact GROUP BY AccountId  HAVING COUNT(Id) > 5

9: Fetch Contact Name, Email, and Account.Industry where Industry = 'Technology'.

SELECT FirstName, LastName, Email, Account.Industry FROM Contact WHERE Account.Industry = 'Technology'

10: Show Account Name and Id for Accounts having at least one Opportunity but no Contact

SELECT Id, Name FROM Account WHERE Id IN (SELECT AccountId FROM Opportunity) AND Id NOT IN (SELECT AccountId FROM Contact)

11: Retrieve Account Name and number of related Contacts (aggregate style)

SELECT AccountId, Account.Name, COUNT(Id) FROM Contact GROUP BY AccountId, Account.Name

12: List Contacts whose CreatedDate is earlier than their parent Account’s CreatedDate

This question might be asked intentionally but you should be aware that SOQL doesn’t allow cross-object field comparison (CreatedDate < Account.CreatedDate) directly.

Workaround (2-step or formula approach):

  • Option 1: Create a formula field on Contact: AccountCreatedDate__c = Account.CreatedDate, then:

    SELECT Id, Name FROM Contact WHERE CreatedDate < AccountCreatedDate__c
  • Option 2: Handle logic in Apex after querying both.

13: Write a query to display each Opportunity Stage with total Amount using GROUP BY

SELECT StageName, SUM(Amount) FROM Opportunity GROUP BY StageName

14: Fetch all Accounts where no Cases exist.

SELECT Name FROM Account WHERE Id NOT IN (SELECT AccountId FROM Case)


Link to Part 2: https://sfdcinterviewpractice.blogspot.com/2025/11/soql-interview-questions-part-2.html

Comments

Popular posts from this blog

SOQL Interview questions - Part 2