Helpful Salesforce SOQL Queries

Data Cleanup & Integrity
Accounts With No Contacts
Finds Accounts without related Contacts, often indicating incomplete or unused data.
SELECT Id, Name
FROM Account
WHERE Id NOT IN (
  SELECT AccountId
  FROM Contact
)
          
Duplicate-ish Accounts
Surfaces potential duplicates based on matching Account names.
SELECT Name, COUNT(Id)
FROM Account
GROUP BY Name
HAVING COUNT(Id) > 1
          
User Adoption & Activity
Users Who Haven’t Logged In Recently
Helps identify inactive users for security reviews and license optimization.
SELECT Id, Name, username, profile.name, LastLoginDate
FROM User
WHERE IsActive = true
AND LastLoginDate < LAST_N_DAYS:90
          
Automation & Logic
Objects With Triggers (non managed packages)
Shows which objects contain Apex triggers.
SELECT Id, Name, TableEnumOrId
FROM ApexTrigger WHERE NamespacePrefix = null
ORDER BY TableEnumOrId, Name
          
Active Flows by Object (non managed packages)
Identifies objects with active record-triggered Flows.
SELECT Id, DurableId, ApiName, Label, Description, ProcessType, TriggerType, NamespacePrefix, TriggerObjectOrEventLabel 
FROM FlowDefinitionView WHERE NamespacePrefix = null and Isactive = true and ProcessType = 'AutoLaunchedFlow' order by TriggerObjectOrEventLabel
          
Tech Debt & Metadata Risk
Objects With High Field Counts
Objects with excessive field counts often signal over-customization or complexity risk.
SELECT EntityDefinition.QualifiedApiName, COUNT(Id)
FROM FieldDefinition
GROUP BY EntityDefinition.QualifiedApiName
HAVING COUNT(Id) > 200