Salesforce SOQL Interview Questions
Salesforce SOQL Interview Questions
Simple Query:
=============
1. SELECT Contact.Firstname, Contact.Account.Name FROM Contact
WHERE Clause
=============
- SELECT Id FROM Contact WHERE Name LIKE 'A%' AND
MailingState='California'
- SELECT Name FROM Account WHERE CreatedDate > 2011-04-26T10:00:00-08:00
You can use date or datetime values, or date literals. The format for date
and dateTime fields are different.
- SELECT Amount FROM Opportunity WHERE CALENDAR_YEAR(CreatedDate) = 2011
==============================================
ID field Semi-Join
Parent-to-child:
You can include a semi-join in a WHERE clause. For example, the following
query returns account IDs if an associated opportunity
is lost:
SELECT Id, Name FROM Account WHERE Id IN ( SELECT AccountId FROM Opportunity
WHERE StageName = 'Closed Lost')
Akshay chowdhry
Salesforce Query
============================================
==========================
ID field Anti-Join
Parent-to-child:
The following query returns account IDs for all accounts that do not have
any open opportunities:
SELECT Id FROM Account WHERE Id NOT IN(SELECT AccountId FROM Opportunity
WHERE IsClosed = false)
============================
Reference Field Anti-Join
Child-to-child:
The following query returns opportunity IDs for all contacts whose source
is not Web:
SELECT Id FROM Opportunity WHERE AccountId NOT IN (SELECT AccountId FROM
Contact WHERE LeadSource = 'Web')
===========================
ORDER BY:
ORDER BY fieldOrderByList {ASC|DESC} [NULLS {FIRST|LAST}] ]
========================================
LIMIT:
OFFSET:
Akshay chowdhry
Salesforce Query
GROUP By:
GROUP BY ROLLUP:
SELECT LeadSource, COUNT(Name) cnt FROM Lead GROUP BY ROLLUP(LeadSource)
================================
HAVING: The HAVING clause is similar to a WHERE clause. The difference is
that you can include aggregate functions in a HAVING clause, but not in a
WHERE clause.
For example: Query returns accounts with duplicate names:
SELECT Name, Count(Id) FROM Account GROUP BY Name HAVING Count(Id) > 1
====================================
AGGREGATE FUNCTIONS:
Two: Count() , Count(fieldName)
If you are using a GROUP BY clause, use COUNT(fieldName) instead of COUNT().
YOu can use count with LIMIT, but you cann't use count() with ORDER BY,
GROUP BY, instead can use count(fieldName)
Akshay chowdhry