Return the list of records:
Account[] lstAcc = [Select Name, Phone FROM Account];
WHERE conditions:
SELECT Id, Name, Phone from Account where Name = 'SFDC Computing'
SELECT Id, Name, Phone from Account where ( Name = 'SFDC Computing' AND Employees > 25)
SELECT Id, Name, Phone from Account where (Name = 'SFDC Computing1' OR ( Name = 'SFDC Computing' AND Employees > 25))''
LIKE :
SELECT Id, Name, Phone from Account where Name LIKE 'SFDC Computing%'
ORDER BY : You can sort on most fields, including numeric and text fields. You can’t sort on fields like rich text and multi-select picklists.
SELECT Id, Name, Phone from Account Where Order by Name
SELECT Id, Name, Phone from Account Where Order by Name ASC
SELECT Id, Name, Phone from Account Where Order by Name DESC
LIMIT :
SELECT Id, Name, Phone from Account LIMIT 1
Ex:
Single :
SELECT Id, Name, Phone from Account Where Name Like 'Dickenson%' Order by Name LIMIT 1
Bulk:
Account[] accts = [SELECT Name,Phone FROM Account
ORDER BY Name
LIMIT 10];
System.debug(accts.size() + ' account(s) returned.');
System.debug(accts);
Inner SOQL:
Account[] acctwithContacts = [Select Name, (Select FirstName, Lastname from Contacts)
from Account
where Name = 'Dickenson plc'];
Contact[] cts = acctwithContacts[0].Contacts;
System.debug('name of first contact:=====> ' +cts[0].FirstName+ ' , ' +cts[0].Lastname);
Insert List of Account with less code:
insert new Account[]{new Account(Name = 'for loop 1'),
new Account(Name = 'for loop 2'),
new Account(Name = 'for loop 3')};
Querying Record in Batches By Using SOQL For Loops :
insert new Account[]{new Account(Name = 'for loop 1'),
new Account(Name = 'for loop 2'),
new Account(Name = 'for loop 3')};
integer i=0;
integer j=0;
for(Account[] temp : [Select id, name from Account where name like 'for loop _']){
j= temp.size();
i++;
}
system.assertEquals(3, j); //returns 3 records
system.assertEquals(1, i); // Since a single batch can hold up to 200 records and, only three records
should have been returned, the loop should have executed only once