What is SOQL ? Types of SOQL - SOQL Basics | Salesforce Funda

 SOQL term as Salesforce Object Query language, This is Salesforce Object Query Language designed to work with SFDC Database. 



SOQL(Salesforce Object Query Language) is used to query the records from the database based on the requirement.  It can search a record on a given criterion only in single sObject.

When to Use SOQL:-

Use SOQL when you know which objects the data resides in, and you want to:
  • Retrieve data from a single object or from multiple objects that are related to one another.
  • Count the number of records that meet specified criteria.
  • Sort results as part of the query.
  • Retrieve data from number, date, or checkbox fields.
SOQL is same as SQL but major difference between SQL and SOQL  :- 
 
    - SQL is used to get the data from one or more tables even if they are not related to each other 
    - SOQL it is used to get the data of a particular object or related object based on certain conditions.

SOQL Syntax :-

  
  Select (fields) From (ObjectAPI) Where (Conditions are met)
  

There are 2 types of SOQL statements :-

1. Static SOQL
2. Dynamic SOQL

Static SOQL :-

In Salesforce, you can use static SOQL queries directly in Apex code without needing to create a separate query variable. Static SOQL queries are enclosed within square brackets and are executed at compile-time rather than runtime. 

This means that the query results are known at compile-time and can be accessed directly without the need for additional processing.

Static SOQL statement is written in [ ] array brackets.

SOQL Syntax :- 

  
   List<Account> accounts = [SELECT Id, Name, Industry FROM Account WHERE Industry = 'Technology' LIMIT 10];
  

Dynamic SOQL :- 

Dynamic SOQL refers to the creation and execution of SOQL queries dynamically at runtime in Salesforce. Unlike static SOQL, dynamic SOQL queries are constructed as strings and can be modified based on the needs of the application or user input.

Dynamic SOQL queries are useful when the query criteria, fields to select, or objects to query are not known until runtime. 

To create a Dynamic SOQL query at runtime use Database.Query() method, in one of the following ways. It returns a single sObject/list of sObject when the query returns a single record/more than a single record.

Here's an example of dynamic SOQL in Apex:

For Example :-

  
String industry = 'Technology';
String query = 'SELECT Id, Name, Industry FROM Account';

if (industry != null) {
    query += ' WHERE Industry = \'' + industry + '\'';
}

query += ' LIMIT 10';

List<Account> accounts = Database.query(query);

SOQL Generic Query :-
  
//Account object Query
Select Id, Name From Account
//Contact object Query
Select Id, Name From Contact
//Opportunity object Query
Select Id, Name from Opportunity

Just remember SOQL syntax :- Select (fields) From (ObjectAPI) Where (Conditions are met)

Where when we run query, it will return Id and Name Fields, when we run query in Query Editor in Developer console