IntermediateLesson 16 of 37
Relationship queries, aggregates and SOSL
Query across objects and search text.
sql
-- child-to-parent (dot notation)
SELECT Name, Account.Name, Account.Industry FROM Contact
-- parent-to-child (subquery on the relationship name)
SELECT Name, (SELECT LastName FROM Contacts) FROM Account
-- aggregates
SELECT StageName, COUNT(Id), SUM(Amount)
FROM Opportunity GROUP BY StageName HAVING SUM(Amount) > 100000SOSL searches text across many objects at once:
sql
FIND 'acme*' IN NAME FIELDS RETURNING Account(Id, Name), Contact(Id, Name)Use SOQL when you know the object; SOSL when you need to search across objects.