–  SOQL (Salesforce object Query Language) retrieves the records from the database by using “SELECT” keyword.

–  By using SOQL, we can know in which object or fields the data resides.

–  We can retrieve the data from single object or from multiple objects that are related to each other.

–  SOQL uses the SELECT statement combined with filtering statements to return sets of data, which may optionally be ordered:

              SELECT one or more fields

              FROM an object

              WHERE filter statements and, optionally, results are ordered

SOQL query is enclosed between square brackets. The following query retrieves a record from database that has the name field value equal to “Airway”.

Account a = [Select ID, Name from Account where Name=’acc1′];

In the above query “a” variable stores the ID, Name of the all accounts with name “acc1”

–  SOQL statements evaluate to a list of SObject records, single SObject records or an integer for count method quires.

Querying Single SObject records: Below query gets single account record with given ID.

         Account a = [Select Name from Account where ID=’XXXXXXXXXXXX’];

Querying List of Sobjects records: Following example is to display list of account.

// querying list of records and stores in list variable “acc”

List<Account>  acc = [Select Name from Account where industry = ‘education’];

System.debug(‘Size: ‘+acc.size());    // displays size of the list

for(Integer i=0; i<=acc.size();i++)


System.debug(‘Size: ‘+acc[i].Name);      // To display accounts stored in “acc”


Below query gets number of record in an object.

Integer i = [Select count() from Account where industry=’education’];

System.debug(‘Count: ‘ + i);