SQL vs SOQL

If you've come from a relational database background and are new to Salesforce, this article should give you a beginning comparison between Structure Query Language (SQL) and Salesforce Object Query Language.

For the definitive description of SOQL, go here.

Here are a few key differences:

General

  • SOQL is only for queries; you cannot Create, Update, or Delete records with SOQL alone
  • Comments are not allowed
  • Semicolons are not allowed
  • Multiple statements are not allowed

SELECT clause

  • There is no SELECT *; you must explicitly list the fields you would like to select.
  • The SELECT clause is limited. You cannot concatenate fields, select literals, or use functions. There are restrictions to aliasing fields.
  • There are no CASE statements. There is a TYPEOF clause, which behaves similar to a CASE statement, but only applies to polymorphic keys.
  • There is no DISTINCT keyword. One can simulate the effect with a GROUP BY.
    E.g. SELECT BillingState FROM Account GROUP BY BillingState
  • SOQL allows a limited set of aggregates.
    For details, see this.
  • SOQL has a number of built-in date functions.
    E.g. SELECT CALENDAR_YEAR(CreatedDate) FROM Opportunity
    For details, see this.
FROM clause
  • There is no JOIN clause, and JOINS cannot be incorporated into the FROM clause. JOINs use a different syntax in SOQL and are generally more limited, though in some cases, easier to use.
  • You cannot list multiple tables. E.g. SELECT Account.Id FROM Account, Contact WHERE Contact.AccountId = Account.Id will generate and error.
WHERE clause
  • There is no JOIN clause, and JOINS cannot be incorporated into the WHERE clause. JOINs can effectively be done by referencing a reference field.
    SELECT Account.Name FROM Contact will JOIN the Account object.
  • The WHERE clause cannot compare two fields. E.g. WHERE CreatedDate < LastModifiedDate will generate an error.
  • You cannot reference Long Text fields or BLOBs in a WHERE clause.
  • You use the equal operator to find nulls. E.g. SELECT Id FROM Account WHERE Fax=NULL. IS NULL will cause an error.
  • DateTimes use a different syntax. E.g. SELECT Id FROM Account WHERE CreatedDate > 2020-06-01T00:00:00Z. Note the lack of quotes.
  • The WHERE clause allow for a number of date functions. See the section above in the SELECT description
GROUP BY/HAVING clause
  • You cannot reference Long Text fields or BLOBs in a GROUP BY or HAVING clause.

 

 

© 2001 – 2023 Object Factory Inc
marker marker marker marker marker