Salesforce Developer – SOQL & SOSL Queries Questions
SOQL and SOSL are essential querying languages in Salesforce that enable developers to retrieve data efficiently. These questions cover the differences between SOQL and SQL, basic SOQL structure, relationship queries, WITH SECURITY_ENFORCED clause, SOSL vs SOQL differences, SOQL limitations, query selectivity concepts, semi-join and anti-join queries, SOQL injection prevention, query execution methods, query plan analysis, aggregate queries, deleted record querying, large data volume considerations, and WITH clause enhancements. Understanding these querying techniques is crucial for effective data retrieval in Salesforce.
SOQL & SOSL Queries - Q&A
- Q1. What is SOQL and how does it differ from SQL?
Ans: SOQL (Salesforce Object Query Language) is a query language specifically designed for retrieving data from Salesforce databases. Key differences from SQL: - SOQL only supports SELECT operations (no INSERT/UPDATE/DELETE) - Works with Salesforce objects rather than database tables - Includes relationship queries (parent-to-child and child-to-parent) - Governed by strict query limits - Q2. Explain the basic structure of a SOQL query.
Ans: The fundamental SOQL syntax consists of: SELECT fields FROM Object [WHERE conditions] [GROUP BY fields] [ORDER BY fields] [LIMIT n] [OFFSET n] All clauses except SELECT and FROM are optional.
Q3. What are relationship queries in SOQL? - Q4. What is the purpose of the WITH SECURITY_ENFORCED clause?
Ans: This clause ensures the query respects field-level security (FLS) by: - Removing fields the user doesn't have access to from SELECT - Filtering records from junction objects where user lacks access - Throwing exceptions if used with aggregate functions or GROUP BY - Q5. How does SOSL differ from SOQL?
Ans: SOSL (Salesforce Object Search Language) is designed for: - Full-text search across multiple objects - Returning multiple object types in a single query - Searching text, email, and phone fields - Using the FIND clause instead of SELECT - Q6. What are the limitations of SOQL?
Ans: Key limitations include: - Maximum 100 SOQL queries in synchronous transactions - No more than 20,000 records returned in a single query - No wildcards in WHERE clauses (except with SOSL) - Limited to querying one object at a time (except with joins) - Q7. Explain the concept of query selectivity.
Ans: Selectivity refers to how efficiently a query can use indexes: - High selectivity: Queries that filter on indexed fields (e.g., Id, external IDs) - Low selectivity: Queries on non-indexed fields or with leading wildcards - Salesforce may reject non-selective queries if they could return > 300,000 records - Q8. What are semi-join and anti-join queries?
Ans: Special SOQL patterns that use ID filtering: - Semi-join: WHERE Id IN (subquery) - Finds records with related records - Anti-join: WHERE Id NOT IN (subquery) - Finds records without related records - Q9. How do you prevent SOQL injection attacks?
Ans: Best practices include: - Using bind variables (:variable) instead of string concatenation - The WITH SECURITY_ENFORCED clause - The stripInaccessible() method for FLS checks - Validating all user input before using in queries - Q10. What are the different ways to execute SOQL queries?
Ans: SOQL can be executed through: - Apex code (inline or dynamic) - Developer Console Query Editor - Workbench or other external tools - REST/SOAP API endpoints - Salesforce CLI - Q11. Explain the concept of query plan in SOQL.
Ans: The query plan shows how Salesforce executes a query: - Available through EXPLAIN in Developer Console - Displays cardinality, cost, and relative cost - Helps identify performance bottlenecks - Shows which indexes are being used - Q12. What are aggregate queries in SOQL?
Ans: Queries that perform calculations on groups of records: - Use functions like COUNT(), SUM(), MAX(), MIN(), AVG() - Require GROUP BY clause for grouping - Can use HAVING to filter groups - Return AggregateResult objects in Apex - Q13. How do you query deleted records in SOQL?
Ans: Using the ALL ROWS clause: SELECT Id FROM Account WHERE IsDeleted = true ALL ROWS - Requires "View All Data" permission - Available for 15 days in Recycle Bin (45 days for admins) - Can be undeleted if within retention period - Q14. What are the considerations for large data volumes in SOQL?
Ans: Important factors include: - Using selective queries with indexed fields - Implementing pagination with OFFSET or LIMIT - Considering Big Objects for archival data - Using FOR VIEW or FOR REFERENCE for read-only cases - Q15. How does the WITH clause enhance SOQL capabilities?
Ans: The WITH clause enables: - DATA CATEGORY filtering for Knowledge articles - NETWORK filtering for Experience Cloud sites - PRICEBOOK ID specification for product queries - TYPEOF for polymorphic field relationships
Ans: Relationship queries allow traversing object relationships: - Child-to-Parent: Uses dot notation (e.g., Account.Name from Contact) - Parent-to-Child: Uses subqueries (e.g., SELECT Name, (SELECT LastName FROM Contacts) FROM Account) - Many-to-Many: Through junction objects