By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
horizontal-light horizontal-dark
  • Home
  • Tutorials
    • Salesforce AI
    • Salesforce DevOps
    • Career
    • Errors
    • Interview Questions
    • Salesforce Integration
    • Salesforce Flow
  • Salesforce Tools
  • Apex Development
  • Lightning Web Components
  • Salesforce Admin
  • About
    • Privacy Policy
    • Disclaimer
    • Terms & Conditions
    • Contact
SalesforceCornerSalesforceCorner
Search
  • Home
  • Tutorials
    • Salesforce AI
    • Salesforce DevOps
    • Career
    • Errors
    • Interview Questions
    • Salesforce Integration
    • Salesforce Flow
  • Salesforce Tools
  • Apex Development
  • Lightning Web Components
  • Salesforce Admin
  • About
    • Privacy Policy
    • Disclaimer
    • Terms & Conditions
    • Contact
Follow US
Salesforce Corner » Apex Development » SOQL Query Examples for Beginners in Salesforce (2026 Guide)
Apex Development

SOQL Query Examples for Beginners in Salesforce (2026 Guide)

Learn how to write SOQL queries in Salesforce with beginner-friendly examples, syntax explanations, and real business scenarios.

Neha Panwar
By
Neha Panwar
ByNeha Panwar
Salesforce Developer and Technical Writer
Neha Panwar is a Salesforce developer and technical writer who shares practical tutorials, Apex guides, and real-world solutions for developers. She focuses on simplifying Salesforce concepts,...
Follow:
- Salesforce Developer and Technical Writer
Last updated: 2026/06/21
Share
SOQL query examples
SHARE

If you want to become a Salesforce Admin or Developer, learning SOQL is absolutely essential.

Almost every Salesforce project uses SOQL queries somewhere. Whether you are writing Apex code, building integrations, creating Lightning Web Components, or debugging records, you will regularly work with SOQL.

However, many beginners struggle because SOQL initially looks similar to SQL, yet Salesforce has its own syntax and rules.

The good news is that SOQL becomes much easier once you start practicing with real examples.

In this beginner-friendly guide, you will learn:

  • what SOQL is,
  • basic SOQL syntax,
  • filtering queries,
  • relationship queries,
  • aggregate queries,
  • and real SOQL examples commonly used in Salesforce projects.

Additionally, this guide will help you understand how SOQL is used inside Apex, Workbench, Developer Console, and Lightning Web Components.

If you are still setting up your Salesforce development environment, you should also read:

  • Salesforce Workbench Tutorial for Beginners (2026 Guide)
  • Salesforce Developer Console Tutorial for Beginners
  • Salesforce CLI Installation and Setup Guide
  • Apex Trigger Tutorial for Beginners in Salesforce (2026 Guide)
  • Lightning Web Components (LWC) Tutorial for Beginners in Salesforce (2026 Guide)

These topics are closely connected with SOQL development.

Contents
What Is SOQL in Salesforce?Why SOQL Is ImportantBasic SOQL SyntaxMain Parts of SOQLSimple SOQL Query ExamplesFetch All AccountsFetch ContactsFetch OpportunitiesUsing WHERE Clause in SOQLExample: Filter Accounts by IndustryExample: Filter Opportunities by AmountUsing LIKE in SOQLExampleUsing LIMIT in SOQLExampleUsing ORDER BY in SOQLExampleUsing AND and OR ConditionsExample Using ANDExample Using ORRelationship Queries in SOQLChild-to-Parent QueryExampleParent-to-Child QueryExampleAggregate Functions in SOQLCOUNT ExampleSUM ExampleAVG ExampleGROUP BY in SOQLExampleHAVING Clause in SOQLExampleDate Filters in SOQLExample: Last 7 DaysExample: This MonthUsing SOQL in ApexExampleDynamic SOQL ExampleExampleCommon Beginner SOQL MistakesQuerying Inside LoopsSelecting Unnecessary FieldsForgetting LIMIT During TestingSOQL Best PracticesDifference Between SOQL and SOSLSOSL ExampleTools for Running SOQL QueriesReal-World SOQL ScenariosFetch Recently Created CasesFetch Top OpportunitiesFetch Contacts Without EmailFinal Thoughts

What Is SOQL in Salesforce?

SOQL stands for Salesforce Object Query Language.

It is a query language used to retrieve data from Salesforce objects.

In simple words, SOQL helps you fetch records from Salesforce databases.

For example:

SELECT Id, Name FROM Account

This query retrieves:

  • Account Id
  • Account Name

from the Account object.

Although SOQL looks similar to SQL, it is designed specifically for Salesforce’s object-based architecture.

Why SOQL Is Important

SOQL is one of the most important Salesforce development skills because developers use it everywhere.

SOQL is commonly used in:

  • Apex classes,
  • Apex triggers,
  • Lightning Web Components,
  • integrations,
  • APIs,
  • Workbench,
  • and Developer Console.

Without SOQL, retrieving Salesforce data becomes extremely difficult.

Basic SOQL Syntax

The basic SOQL syntax is:

SELECT Fields
FROM Object
WHERE Condition

Main Parts of SOQL

ClausePurpose
SELECTFields to retrieve
FROMObject name
WHEREFilter records
ORDER BYSort results
LIMITRestrict records

Simple SOQL Query Examples

Fetch All Accounts

SELECT Id, Name FROM Account

This retrieves all Account records.

Fetch Contacts

SELECT Id, FirstName, LastName FROM Contact

This retrieves Contact names.

Fetch Opportunities

SELECT Id, Name, Amount FROM Opportunity

This retrieves Opportunity records with Amount values.

Using WHERE Clause in SOQL

The WHERE clause filters records.

Example: Filter Accounts by Industry

SELECT Id, Name
FROM Account
WHERE Industry = 'Technology'

Only Technology Accounts are returned.

Example: Filter Opportunities by Amount

SELECT Name, Amount
FROM Opportunity
WHERE Amount > 10000

This retrieves Opportunities greater than 10,000.

Using LIKE in SOQL

LIKE helps search partial values.

Example

SELECT Name
FROM Account
WHERE Name LIKE 'Sales%'

This retrieves Accounts starting with “Sales”.

Using LIMIT in SOQL

LIMIT restricts returned records.

Example

SELECT Name
FROM Contact
LIMIT 5

Only 5 records are returned.

LIMIT is very useful while testing queries.

Using ORDER BY in SOQL

ORDER BY sorts records.

Example

SELECT Name, CreatedDate
FROM Case
ORDER BY CreatedDate DESC

This retrieves newest Cases first.

Using AND and OR Conditions

Example Using AND

SELECT Name
FROM Account
WHERE Industry = 'Banking'
AND BillingCountry = 'USA'

Example Using OR

SELECT Name
FROM Lead
WHERE Status = 'Open'
OR Status = 'Working'

Relationship Queries in SOQL

Relationship queries are extremely important in Salesforce.

There are two types:

  • Child-to-Parent
  • Parent-to-Child

Child-to-Parent Query

Use dot notation to access parent records.

Example

SELECT Id, FirstName, Account.Name
FROM Contact

This retrieves:

  • Contact Name
  • Parent Account Name

Parent-to-Child Query

Use subqueries for child records.

Example

SELECT Name,
(SELECT LastName FROM Contacts)
FROM Account

This retrieves:

  • Account Name
  • Related Contacts

Relationship queries are heavily used in real Salesforce projects.

Aggregate Functions in SOQL

Aggregate functions perform calculations.

COUNT Example

SELECT COUNT()
FROM Contact

Counts total Contacts.

SUM Example

SELECT SUM(Amount)
FROM Opportunity

Calculates total Opportunity revenue.

AVG Example

SELECT AVG(Amount)
FROM Opportunity

Calculates average Opportunity Amount.

GROUP BY in SOQL

GROUP BY groups records.

Example

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

This groups Accounts by Industry.

HAVING Clause in SOQL

HAVING filters grouped results.

Example

SELECT Industry, COUNT(Id)
FROM Account
GROUP BY Industry
HAVING COUNT(Id) > 5

Only Industries with more than 5 Accounts are returned.

Date Filters in SOQL

Salesforce provides special date literals.

Example: Last 7 Days

SELECT Id, Name
FROM Case
WHERE CreatedDate = LAST_N_DAYS:7

Example: This Month

SELECT Id, Name
FROM Opportunity
WHERE CloseDate = THIS_MONTH

These filters are commonly used in reporting and automation.

Using SOQL in Apex

SOQL is heavily used inside Apex.

Example

List<Account> accounts = [
SELECT Id, Name
FROM Account
WHERE Industry = 'Technology'
];

This retrieves Technology Accounts inside Apex code.

Related guide:
Apex Trigger Tutorial for Beginners in Salesforce.

Dynamic SOQL Example

Dynamic SOQL builds queries at runtime.

Example

String query = 'SELECT Id, Name FROM Account';
List<Account> accs = Database.query(query);

Dynamic SOQL is useful when queries depend on runtime conditions.

Common Beginner SOQL Mistakes

Querying Inside Loops

Bad Example:

for(Account acc : accounts){
Contact con = [
SELECT Id FROM Contact LIMIT 1
];
}

This can hit governor limits.

Always avoid SOQL inside loops.


Selecting Unnecessary Fields

Query only fields you actually need.

Large queries reduce performance.

Forgetting LIMIT During Testing

Without LIMIT, queries may return huge amounts of data.

SOQL Best Practices

To write optimized SOQL queries:

  • Query only required fields
  • Avoid SOQL inside loops
  • Use selective WHERE conditions
  • Use LIMIT while testing
  • Optimize relationship queries
  • Avoid unnecessary large data retrieval
  • Use indexed fields whenever possible

Additionally, bulkified SOQL becomes extremely important in Apex development.

Difference Between SOQL and SOSL

Many beginners confuse SOQL and SOSL.

SOQLSOSL
Retrieves structured recordsSearches text across objects
One object mainlyMultiple objects
Uses SELECTUses FIND
Better for exact queriesBetter for keyword search

SOSL Example

FIND 'Sales*'
RETURNING Account(Name), Contact(Name)

SOSL is useful for text searching across multiple objects.

Tools for Running SOQL Queries

You can run SOQL queries using:

  • Salesforce Workbench
  • Developer Console
  • VS Code
  • Salesforce Inspector
  • Apex code

Real-World SOQL Scenarios

Fetch Recently Created Cases

SELECT CaseNumber, Status
FROM Case
WHERE CreatedDate = TODAY

Fetch Top Opportunities

SELECT Name, Amount
FROM Opportunity
ORDER BY Amount DESC
LIMIT 5

Fetch Contacts Without Email

SELECT FirstName, LastName
FROM Contact
WHERE Email = NULL

These are very common real-world queries.

Final Thoughts

SOQL is one of the most important skills in Salesforce development.

Initially, the syntax may look confusing. However, once you start practicing real examples, SOQL becomes much easier to understand.

Start with:

  • simple SELECT queries,
  • then learn filters,
  • relationship queries,
  • aggregate functions,
  • and Apex integration.

Most importantly, practice regularly inside:

  • Workbench,
  • Developer Console,
  • or VS Code.

Real hands-on experience improves SOQL skills much faster than theory alone.

If you want to continue learning Salesforce development, you should also explore:

  • Related tutorials:
  • Salesforce Workbench Tutorial for Beginners (2026 Guide)
  • Salesforce Developer Console Tutorial for Beginners
  • Salesforce Inspector Chrome Extension Guide for Beginners

These topics will strengthen your Salesforce developer skills significantly.

TAGGED:Apex SOQLSalesforce Beginner Guidesalesforce developerSalesforce QueriesSalesforce SOQLSOQLSOQL Tutorial
Share This Article
Facebook Email Print
ByNeha Panwar
Salesforce Developer and Technical Writer
Follow:
Neha Panwar is a Salesforce developer and technical writer who shares practical tutorials, Apex guides, and real-world solutions for developers. She focuses on simplifying Salesforce concepts, integrations, and backend development to help beginners and professionals learn faster.
Leave a Comment Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Latest Post

Salesforce dynamic forms interface illustration
Salesforce Dynamic Forms: A Better Way to Show and Hide Fields
Salesforce Flow
Salesforce flow debugging guide
How to Debug and Fix Salesforce Flow Errors ?
Salesforce Flow
Salesforce Flow Loops tutorial diagram
Salesforce Flow Loops: Collections, Iteration, and Best Practices
Salesforce Flow
WhoId vs WhatId in Salesforce
WhoId vs WhatId in Salesforce: What’s the Difference and When Should You Use Each?
Apex Development
Salesforce Apex string methods tutorial
20 Apex String Methods Every Salesforce Developer Should Know
Apex Development

Stay Updated with Salesforce Tutorials

Get the latest Salesforce guides, tutorials, and developer tips delivered to your inbox.
slaesforce corner mascot

Explore More Topics

  • salesforce admin
  • salesforce developer
  • Salesforce Admin
  • salesforce apex
  • Salesforce Development
  • salesforce tutorial
  • salesforce security
  • salesforce automation
  • Apex Development
  • lightning web components
  • Lightning Web Components
  • salesforce lwc
  • Salesforce Tutorials
  • Salesforce Tools
  • lwc tutorial
horizontal-dark-transparent

Learn Salesforce development with practical tutorials, Apex guides, integration examples, and real-world solutions for developers.

  • Quick Links:
  • About
  • Contact
  • Privacy Policy
  • Disclaimer
  • Terms & Conditions
Facebook Twitter Youtube Linkedin-in

Salesforce Corner © 2026

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?