How to write SOQL queries in Salesforce

There are many ways to write and execute SOQL (Salesforce Object Query Language) in Salesforce such as Workbench. Just login using your Salesforce Credentials (you have access to Workbench) and start writing and executing SOQL queries. You may either use the query wizard or can directly write query in SOQL query box and execute the same. Some of the sample SOQL queries are below.

A simple SELECT statement:
SELECT AccountNumber, AccountSource FROM Account

This query is valid in most of the database query languages such as SQL but it is invalid for SOQL:
SELECT * FROM Account (This is a invalid query in SOQL)

WHERE clause with numeric check:
SELECT ActualCost,AmountAllOpportunities,AmountWonOpportunities,BudgetedCost FROM Campaign where Actualcost=11400

WHERE clause with string check:
SELECT ActualCost, AmountAllOpportunities, AmountWonOpportunities, BudgetedCost, Description, Name FROM Campaign where Name = 'GC Product Webinar - Jan 7, 2002'

LIKE queries:
SELECT ActualCost, AmountAllOpportunities, AmountWonOpportunities, BudgetedCost, Description, Name FROM Campaign where Name like '%GC Product%'

Using built-in functions:
SELECT Min(BudgetedCost), Max(BudgetedCost) FROM Campaign

This entry was posted in Salesforce, SOQL. Bookmark the permalink.

Leave a Reply