SOQL SELECT Examples

The following are examples of text searches that use SOQL.

Type of SearchExamples
Simple querySELECT Id, Name, BillingCity FROM Account
WHERESELECT Id FROM Contact WHERE Name LIKE 'A%' AND MailingCity = 'California'
ORDER BYSELECT Name FROM Account ORDER BY Name DESC NULLS LAST
LIMITSELECT Name FROM Account WHERE Industry = 'media' LIMIT 125
ORDER BY with LIMITSELECT Name FROM Account WHERE Industry = 'media' ORDER BY BillingPostalCode ASC NULLS LAST LIMIT 125
count()SELECT COUNT() FROM Contact
GROUP BYSELECT LeadSource, COUNT(Name) FROM Lead GROUP BY LeadSource
HAVINGSELECT Name, COUNT(Id) FROM Account GROUP BY Name HAVING COUNT(Id) > 1
OFFSET with ORDER BYSELECT Name, Id FROM Merchandise__c ORDER BY Name OFFSET 100
OFFSET with ORDER BY and LIMITSELECT Name, Id FROM Merchandise__c ORDER BY Name LIMIT 20 OFFSET 100
Relationship queries: child-to-parentSELECT Contact.FirstName, Contact.Account.Name FROM Contact
Relationship queries: parent-to-childSELECT Name, (SELECT LastName FROM Contacts) FROM Account
Relationship query with WHERESELECT Name, (SELECT LastName FROM Contacts WHERE CreatedBy.Alias = 'x') FROM Account WHERE Industry = 'media'
Relationship query: child-to parent with custom objectsSELECT Id, FirstName__c, Mother_of_Child__r.FirstName__c FROM Daughter__c WHERE Mother_of_Child__r.LastName__c LIKE 'C%'
Relationship query: parent to child with custom objectsSELECT Name, (SELECT Name FROM Line_Items__r) FROM Merchandise__c WHERE Name LIKE ‘Acme%’
Relationship queries with polymorphic keySELECT Id, Owner.Name FROM Task WHERE Owner.FirstName like 'B%'
Polymorphic relationship queries using TYPEOFSELECT TYPEOF What WHEN Account THEN Phone, NumberOfEmployees WHEN Opportunity THEN Amount, CloseDate ELSE Name, Email END FROM Event
Relationship queries with aggregateSELECT Name, (SELECT CreatedBy.Name FROM Notes) FROM Account
Simple query: the UserId and LoginTime for each userSELECT UserId, LoginTime from LoginHistory
Relationship queries with number of logins per user in a specific time rangeSELECT UserId, COUNT(Id) from LoginHistory WHERE LoginTime > 2010-09-20T22:16:30.000Z AND LoginTime < 2010-09-21T22:16:30.000Z GROUP BY UserId
Query to retrieve the RecordType per userSELECT Id, Name, IsActive, SobjectType, DeveloperName, Description FROM RecordType

Additional relationship query example (child-to-parent):

1SELECT Id, Name, Account.Name FROM Contact
2WHERE Account.Industry = 'media'

Additional relationship query example (parent-to-child):

1SELECT
2Account.Name, (SELECT Contact.LastName FROM Account.Contacts)
3FROM Account

Additional relationship query examples with polymorphic key:

1SELECT Id, Who.FirstName, Who.LastName FROM Task WHERE
2Owner.FirstName LIKE 'B%'
1SELECT Id, What.Name FROM
2Event

Additional relationship query example with aggregate:

1SELECT
2Amount, Id, Name, (SELECT Quantity, ListPrice,
3PricebookEntry.UnitPrice, PricebookEntry.Name FROM
4OpportunityLineItems) FROM Opportunity

Note: If a user doesn’t have Read access to the object to which a record type belongs, that record type isn’t returned in the query results.

Apex requires that you surround SOQL and SOSL statements with square brackets to use them in your statements. You can use Apex script variables and expressions when preceded by a colon (:).

Note