← Back to list

SOQL Query with Examples

In the Salesforce ecosystem, data is everything. Whether you’re building Triggers, Lightning Web Components (LWC), REST APIs, Batch Jobs…

Kalpesh Koli · 2026-03-01 09:00 · 0 claps · 1.3 min read
#salesforce #soql #salesforce-soql #salesforce-inspector #trailhead
Open on Medium ↗
Wiki topics: CRM · Email & CRM

SOQL Query with Examples

In the Salesforce ecosystem, data is everything. Whether you’re building Triggers, Lightning Web Components (LWC), REST APIs, Batch Jobs, or Reports, you must know how to efficiently retrieve and manipulate data

That’s where SOQL (Salesforce Object Query Language) comes in.

Use Cases :

  • Retrieve records from objects like Account, Contact, Opportunity
  • Filter data using conditions
  • Perform aggregate calculations
  • Limit and sort results

Basic Filtering Queries

  1. Get Accounts from Energy Industry
SELECT Name, Phone, Type, AnnualRevenue, Industry
FROM Account
WHERE Industry = 'Energy'

2.Multiple Conditions (IN + AND)

SELECT Name, Phone, Type, AnnualRevenue, Industry
FROM Account
WHERE Industry IN ('Energy','Education')
AND AnnualRevenue >= 5000

Pattern Matching & Limiting

3.Only 2 Accounts from Energy

SELECT Name
FROM Account
WHERE Industry = 'Energy'
LIMIT 2

4.Name Starts With E

SELECT Name
FROM Account
WHERE Name LIKE 'E%'

5.Name Ends With A

SELECT Name
FROM Account
WHERE Name LIKE '%A'

Range & OR Conditions

6.Employee Range OR Revenue Filter

SELECT Name, Phone, Type, Industry
FROM Account
WHERE (NumberOfEmployees >= 5000 
AND NumberOfEmployees <= 150000)
OR AnnualRevenue >= 500000

Aggregate Queries

7.Get Unique Industry Values (Using Set)

Set<String> industries = new Set<String>();

for(Account acc : [SELECT Industry FROM Account]){
    industries.add(acc.Industry);
}

8.Industry-wise Account Count

SELECT Industry, COUNT(Id)
FROM Account
GROUP BY Industry

Relationship Queries

9.Contact with Account Details (Employees < 25)

SELECT Id, FirstName, LastName, Email,
       Account.Name, Account.NumberOfEmployees
FROM Contact
WHERE Account.NumberOfEmployees < 25

10.Accounts Created This Month with Contacts

SELECT Name, Rating,
       (SELECT FirstName, LastName FROM Contacts)
FROM Account
WHERE CreatedDate = THIS_MONTH
AND Rating IN ('Hot','Cold')

11.Accounts with No Opportunities & No Cases

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

If you practice these in Developer Console or Data Export, you’ll build strong confidence in SOQL Query


메타데이터
post_id
3a4c1df9779b
slug
soql-query-with-examples-3a4c1df9779b
url
https://medium.com/@iamkalpesh0/soql-query-with-examples-3a4c1df9779b
canonical_url
https://medium.com/@iamkalpesh0/soql-query-with-examples-3a4c1df9779b
author_url
https://medium.com/@iamkalpesh0
status
ok
fetched_at
2026-06-21 19:25:17