SOQL Flashcards

1
Q

What is the basic SOQL statement?

A

[SELECT fieldNames FROM objectName]

  • fieldNames is the API name of the field we want to retrieve or comma-separated list of API names of the fields we want to retrieve
  • objectName is the API name of the object that we want to execute our query against
  • we must include at least one field in the SELECT clause of our query and exactly one object in the FROM clause of our query
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the optional SOQL clauses we can use?

A
  • WHERE
  • WITH
  • GROUP BY/HAVING
  • ORDER BY
  • LIMIT
  • OFFSET
  • FOR REFERENCE
  • FOR UPDATE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the WHERE clause do?

A

it’s a filter for which records we want to return

List edgeAccs = [

SELECT Name

FROM Account

WHERE Name = ‘Edge Communications’];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the WITH SECURITY_ENFORCED clause do?

A
  • a query using this clause will take the object and field level permissions of the user executing the query into account
  • if the query is operating on a field or object that the running user doesn’t have read access to, an exception will be thrown and nothing will be returned
  • this is one of three ways we can enforce object and field level security in our Apex code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the two main SOQL return types?

A
  1. a single sObject (a single record, e.g. we can write queries with a return type of Account)
  2. a List of sObjects (e.g. List)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the purpose of Variable Binding in SOQL?

A
  • Variable Binding allow us to make use of variables from Apex as filters
  • to perform variable binding, precede name of variable with “:”

String accountName = ‘Edge Communications’;
List edgeCommunications = [

SELECT Id

FROM Account

WHERE Name=:accountName]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the six aggregate functions that we can use in SOQL queries?

A
  1. COUNT,
  2. COUNT_DISTINCT,
  3. MIN,
  4. MAX,
  5. SUM,
  6. AVG

NOTE: when we write a SOQL query that only contains the COUNT function in its SELECT clause (without an argument) and no grouping, the return type of the query will be Integer

Integer numOfContacts = [SELECT COUNT() FROM Contact];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the purpose of the get() method in AggregateResult?

A

the AggregateResult class has a get() method that we use to retrieve values because we can’t reference any fields on an AggregateResult through dot notation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the FORMAT function?

A

the FORMAT function applies formatting corresponding to a user’s locale to standard/custom number, date, time, and currency fields

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the Date function?

A

date functions allow us to filter or group results by specified dates

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Child-to-Parent Queries

A
  • to retrieve a field from a parent record, we use dot notation
  • if we’re querying a custom relationship, the __c suffix on the relationship field in the query will change __r
  • if we’re querying a standard relationship, these tend to follow the naming format ParentObjectId and we’ll lop off the Id at the end of the field API name (e.g. Account.Name)

List cons = [SELECT Id, Account.Name FROM Contact];List ties = [SELECT Id, Star_Destroyer__r.Name FROM Tie_Fighter__c];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Parent-to-Child Queries

A
  • this is another SOQL query with its own SELECT and FROM clauses that lives in the SELECT clause of the outer query
  • in a subquery, the name of the child object after the FROM clause is pluralized
    • for standard relationships, this generally just means adding an s (e.g. Contacts)
    • for custom relationships, the suffix of the object name will change from __c to __r as well
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are SOQL for loops used for?

A

SOQL for loops are used to avoid governor limits on heap size and the number of queries executed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly