SOQL Query Examples: 10 That Answer Real Questions
SOQL query examples for questions reports dodge: stale deals, inactive owners, duplicate emails, stage history, and the null checks that skew counts.
Most lists of SOQL query examples teach syntax: here is SELECT, here is WHERE, here is ORDER BY. That’s fine for an exam and useless on a Tuesday when your VP asks which open deals nobody has touched in a month.
My position: SOQL is worth learning by question, not by clause. Every query below answers something a sales or ops leader actually asks, and each one carries a trap that makes a correct-looking query return the wrong answer. Whether you write these yourself or have an AI write them through Sentinel Workspace, the trap is the part you need to know.
Field API names are standard objects where possible. Swap in your own custom fields.
1. Which open deals has nobody touched in 30 days?
SELECT Id, Name, Owner.Name, StageName, LastActivityDate
FROM Opportunity
WHERE IsClosed = false
AND (LastActivityDate < LAST_N_DAYS:30 OR LastActivityDate = null)
ORDER BY LastActivityDate ASC NULLS FIRST
The trap is the OR LastActivityDate = null. A deal with no logged activity at all has an empty date, and a date comparison never matches an empty field. Without that clause, the deals nobody has ever touched, the worst ones, drop off your list. The full pattern is in testing what SOQL null filters drop.
2. Which open deals belong to people who left?
SELECT Id, Name, Amount, Owner.Name
FROM Opportunity
WHERE IsClosed = false
AND Owner.IsActive = false
Child-to-parent dot notation (Owner.IsActive) is the most useful thing in SOQL that report builders hide. Deactivating a user doesn’t move their pipeline, so this query finds revenue sitting with nobody. If it returns rows every month, the fix is an offboarding process that reassigns records, not a monthly cleanup.
3. Which accounts have no open opportunity?
SELECT Id, Name, Owner.Name
FROM Account
WHERE Type = 'Customer'
AND Id NOT IN (
SELECT AccountId FROM Opportunity WHERE IsClosed = false
)
This is an anti-join: records on one object that have no matching record on another. Reports can approximate it with cross filters, but the query states the question exactly: customers with nothing in the pipeline. That’s your expansion list, or your churn risk list, depending on who’s asking.
4. Which contacts share an email address?
SELECT Email, COUNT(Id)
FROM Contact
WHERE Email != null
GROUP BY Email
HAVING COUNT(Id) > 1
HAVING filters on the aggregate, which is how you find the duplicates instead of just counting contacts. The Email != null matters: without it, every contact with no email lands in one giant “duplicate” group and buries the real ones. Once you see the scale, duplicate rules built around your actual matching logic keep the list from refilling.
5. What’s in each customer’s open pipeline?
SELECT Name,
(SELECT Name, StageName, Amount
FROM Opportunities
WHERE IsClosed = false)
FROM Account
WHERE Type = 'Customer'
The parent-to-child subquery uses the relationship name (Opportunities, plural), not the object name. For custom objects that relationship name ends in __r. This shape is what you want before an account review: one row per account with its deals nested inside, rather than a flat export you have to pivot.
6. Which deals moved backwards a stage this week?
SELECT OpportunityId, Opportunity.Name, OldValue, NewValue,
CreatedBy.Name, CreatedDate
FROM OpportunityFieldHistory
WHERE Field = 'StageName'
AND CreatedDate = LAST_N_DAYS:7
ORDER BY CreatedDate DESC
History objects record every tracked change, which makes them the only honest source for “what changed.” Two traps. Field history only exists for fields you turned tracking on for, and you can’t filter on OldValue or NewValue, so you pull the week’s stage changes and compare them in a script or a sheet. The setup side is covered in the field history tracking guide.
7. Which leads are still sitting in a queue?
SELECT Id, Name, Owner.Name, CreatedDate, LeadSource
FROM Lead
WHERE IsConverted = false
AND Owner.Type = 'Queue'
AND CreatedDate < LAST_N_DAYS:2
Lead owner can be a user or a queue, and Owner.Type tells you which. Any lead still owned by a queue two days after arriving is a lead nobody claimed. If this list is long, the problem is usually distribution, and round robin lead assignment is the build that empties it.
8. Where did this quarter’s converted leads come from?
SELECT LeadSource, COUNT(Id)
FROM Lead
WHERE IsConverted = true
AND ConvertedDate = THIS_QUARTER
GROUP BY LeadSource
Date literals like THIS_QUARTER and LAST_N_DAYS:n follow your org’s fiscal settings and time zone, which is why they beat hard-coded dates. Salesforce’s date literal reference lists them all, and notes that LAST_N_DAYS:n includes today. Watch for a blank LeadSource bucket in the results; its size tells you how much attribution you’re missing.
9. Which accounts sell a specific service?
SELECT Id, Name, Services__c
FROM Account
WHERE Services__c INCLUDES ('Onboarding', 'Training')
Multi-select picklists need INCLUDES, not =. The comma means “either”: accounts with Onboarding or Training. To require both, put them in one string separated by a semicolon: INCLUDES ('Onboarding;Training'). Mixing those two up is the most common reason a segment count looks wrong.
10. How many records would this change touch?
SELECT COUNT()
FROM Contact
WHERE MailingCountry = null
AND Account.BillingCountry != null
Run a count before every bulk change. Here, it’s contacts with no country whose account has one, the candidates for a backfill. If the number is 40 and you expected 4,000, your filter is wrong, and you found out for free. That habit is the difference between a bulk update without Data Loader and a bad afternoon.
What SOQL won’t answer
SOQL can’t compare two fields on the same record. WHERE CloseDate < CreatedDate is not valid; you add a formula field that does the comparison and filter on that. It also has no free-form joins, can’t filter on long text area fields, and is read-only. A query finds the records. Changing them is a separate step, and that’s where most of the risk sits.
That’s also the line between asking and building. A query answers this week’s question. A report your org won’t build, an alert when a deal moves backwards, or a nightly job that reassigns orphaned pipeline answers it every week without anyone remembering to ask.
Where your AI fits
You don’t have to memorize any of this. If you’d rather describe the question and read the result, the plain-English approach to SOQL shows how to check what an AI wrote before you trust it.
The bigger step is letting that AI build the fix, not just find the problem. Sentinel connects your own AI (Claude, or any assistant that speaks MCP) to your Salesforce org, so it can run these queries, then write and deploy the automation that stops them returning rows, sandbox first, with every change logged and a snapshot before each deploy. That doesn’t stop a bad change from happening. It means you can see exactly what changed and get back to where you were. The Sentinel for Salesforce overview covers what teams build first.
The takeaway
Pick three of these and run them this week: stale deals with the null check, pipeline owned by inactive users, and leads stuck in a queue. Whatever comes back is your list of processes that are leaking.
Sentinel Workspace lets your AI turn each of those queries into a build that fixes the leak for good. Pricing is flat per Sentinel and is covered on a short demo call.
KEEP READING
Salesforce Validation Rule Examples: 8 Worth Building
Salesforce validation rule examples that guard changes, not blank fields: eight formulas worth building, plus where validation rules stop working.
Salesforce Data Cleanup Automation: Sort First, Fix Second
Salesforce data cleanup automation stalls on decisions, not tools. Sort every record into fix, review or retire, then make each change reversible.
See Sentinel Workspace build in your own CRM on a short demo call.
Book a Demo Call