{"id":1487,"date":"2026-09-04T19:00:00","date_gmt":"2026-09-04T13:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/soql-relationship-queries-parent-child\/"},"modified":"2026-09-04T19:00:00","modified_gmt":"2026-09-04T13:30:00","slug":"soql-relationship-queries-parent-child","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/soql-relationship-queries-parent-child\/","title":{"rendered":"SOQL Relationship Queries: Parent and Child Records in Apex"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">SOQL Relationship Queries let you read related records in one query, such as each enrolment with its course name, or each course with all its enrolments. Instead of running two queries and matching results by hand, you follow the relationship inside the query itself.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this post, you will learn both directions of relationship queries, how relationship names work, and how semi-joins filter by related data. It builds on the <a href=\"https:\/\/swapnilzambare.com\/blog\/salesforce-data-model-explained-objects-fields-and-relationships-for-beginners\/\">Salesforce data model<\/a> and <a href=\"https:\/\/swapnilzambare.com\/blog\/master-detail-vs-lookup-salesforce\/\">master-detail vs lookup relationships<\/a>. It also continues from the <a href=\"https:\/\/swapnilzambare.com\/blog\/soql-where-clause-order-by-limit\/\">SOQL WHERE clause guide<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are SOQL Relationship Queries?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SOQL Relationship Queries read fields from related objects in a single query. There are two directions. Child-to-parent queries read fields from a parent, such as the course of an enrolment. Parent-to-child queries read a list of children, such as all enrolments of a course.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a school register. From a student&#8217;s row, you can read the class teacher&#8217;s name, which is child to parent. From the class page, you can read every student in the class, which is parent to child. Both views use the same link between student and class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why SOQL Relationship Queries Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Every extra query uses one of your 100 allowed queries. SOQL Relationship Queries reduce that count and keep code simple. Here is how they help at Bright Path Tutors.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, the enrolment email needs the course name and start date.<\/li>\n\n<li>Also, the trainer dashboard lists each course with its active students.<\/li>\n\n<li>Next, a class must find courses that have no enrolments yet.<\/li>\n\n<li>A report shows each enrolment with the student&#8217;s account name.<\/li>\n\n<li>Finally, triggers must read parent data without a query per record.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">So one well-written query often replaces two or three simple ones.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How SOQL Relationship Queries Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SOQL Relationship Queries use relationship names. First, you find the relationship name on the lookup or master-detail field. Next, for child-to-parent, you use dot notation, such as <code>Course__r.Name<\/code>. Then, for parent-to-child, you write a subquery in brackets using the child relationship name, such as <code>Enrolments__r<\/code>. Finally, Apex returns parent records with their related fields or child lists attached.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-relationship-queries-parent-child-diagram-1-1024x640.png\" alt=\"SOQL Relationship Queries diagram showing relationship names, dot notation, subqueries and semi-joins\" class=\"wp-image-1485\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-relationship-queries-parent-child-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-relationship-queries-parent-child-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-relationship-queries-parent-child-diagram-1-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-relationship-queries-parent-child-diagram-1-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-relationship-queries-parent-child-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">How SOQL follows relationships in both directions.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Child-to-Parent with Dot Notation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">On a custom lookup field named <code>Course__c<\/code>, the relationship name is <code>Course__r<\/code>. So <code>Course__r.Name<\/code> reads the parent course name. For standard fields, you drop the suffix, such as <code>Account.Name<\/code> on Contact. You can go up to five levels, such as <code>Course__r.Trainer__r.Name<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parent-to-Child with Subqueries<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A subquery sits inside the SELECT list, in brackets. It uses the child relationship name, which is usually plural, such as <code>Enrolments__r<\/code> or <code>Contacts<\/code>. Each parent record then carries a list of its children.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Finding Relationship Names<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Open Object Manager, then the child object, then the lookup field. The field details show the Child Relationship Name, such as &#8220;Enrolments&#8221;. In SOQL, custom names add <code>__r<\/code>, so it becomes <code>Enrolments__r<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Semi-Joins and Anti-Joins<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A semi-join filters parents by their children, such as <code>WHERE Id IN (SELECT Course__c FROM Enrolment__c)<\/code>. An anti-join uses <code>NOT IN<\/code> to find parents with no matching children.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Relationship Names at a Glance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below shows how names change between fields and queries.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Field or relationship<\/th><th>API name<\/th><th>Name in SOQL<\/th><\/tr><\/thead><tbody><tr><td>Custom lookup on Enrolment<\/td><td>Course__c<\/td><td>Course__r.Name<\/td><\/tr><tr><td>Lookup on Course to a user<\/td><td>Trainer__c<\/td><td>Trainer__r.Name<\/td><\/tr><tr><td>Standard lookup on Contact<\/td><td>AccountId<\/td><td>Account.Name<\/td><\/tr><tr><td>Custom child list on Course<\/td><td>Enrolments<\/td><td>Enrolments__r<\/td><\/tr><tr><td>Standard child list on Account<\/td><td>Contacts<\/td><td>Contacts<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Child-to-Parent vs Parent-to-Child<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below compares the two directions of SOQL Relationship Queries.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Child-to-parent<\/th><th>Parent-to-child<\/th><\/tr><\/thead><tbody><tr><td>Syntax<\/td><td>Dot notation, Course__r.Name<\/td><td>Subquery, (SELECT &#8230; FROM Enrolments__r)<\/td><\/tr><tr><td>Starts from<\/td><td>The child object<\/td><td>The parent object<\/td><\/tr><tr><td>Returns<\/td><td>One parent value per row<\/td><td>A list of children per row<\/td><\/tr><tr><td>Depth<\/td><td>Up to five levels up<\/td><td>Usually one level down in Apex<\/td><\/tr><tr><td>Example<\/td><td>Enrolment with course name<\/td><td>Course with its enrolments<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When to use which depends on your starting point. Start from the child when you need parent details. Start from the parent when you need its list of children.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-relationship-queries-parent-child-diagram-2-1024x640.png\" alt=\"SOQL Relationship Queries diagram comparing child-to-parent and parent-to-child queries\" class=\"wp-image-1486\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-relationship-queries-parent-child-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-relationship-queries-parent-child-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-relationship-queries-parent-child-diagram-2-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-relationship-queries-parent-child-diagram-2-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-relationship-queries-parent-child-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Child-to-parent vs parent-to-child queries.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">A Real SOQL Relationship Queries Example in Apex<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s build two queries for Bright Path Tutors. The Enrolment__c object has a lookup to Course__c and a lookup to Contact called Student__c.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Child-to-Parent Query<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;Enrolment__c&gt; enrolments = [\n    SELECT Id, Status__c,\n           Student__r.Name,\n           Course__r.Name, Course__r.Start_Date__c,\n           Course__r.Trainer__r.Name\n    FROM Enrolment__c\n    WHERE Status__c = 'Active'\n    LIMIT 5\n];\n\nfor (Enrolment__c e : enrolments) {\n    System.debug(e.Student__r.Name + ' joined ' + e.Course__r.Name\n        + ' with trainer ' + e.Course__r.Trainer__r.Name);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The query starts from Enrolment__c. Dot notation reads the student name, the course name and start date, and even the trainer name two levels up. Inside the loop, we read those fields with the same dot notation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Parent-to-Child Query<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;Course__c&gt; courses = [\n    SELECT Id, Name,\n           (SELECT Id, Student__r.Name, Status__c\n            FROM Enrolments__r\n            WHERE Status__c = 'Active'\n            ORDER BY CreatedDate)\n    FROM Course__c\n    WHERE Is_Online__c = true\n];\n\nfor (Course__c c : courses) {\n    System.debug(c.Name + ' has ' + c.Enrolments__r.size() + ' active students');\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The subquery returns each course&#8217;s active enrolments. In Apex, <code>c.Enrolments__r<\/code> is a list, so <code>size()<\/code> counts the students. The subquery can have its own WHERE and ORDER BY.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Read the Output<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an example of the debug log for Step 2.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>USER_DEBUG [12]|DEBUG|Apex Basics has 18 active students\nUSER_DEBUG [12]|DEBUG|SOQL Basics has 0 active students\nUSER_DEBUG [12]|DEBUG|LWC Basics has 7 active students<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Find Courses with No Enrolments<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;Course__c&gt; emptyCourses = [\n    SELECT Id, Name\n    FROM Course__c\n    WHERE Id NOT IN (SELECT Course__c FROM Enrolment__c)\n];\nSystem.debug('Courses without enrolments: ' + emptyCourses.size());<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This anti-join is one of the most useful SOQL Relationship Queries. It returns courses that no enrolment points to. Also, it is one query, with no loops or maps needed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SOQL Relationship Queries and Limits<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SOQL Relationship Queries still count as one query each. However, the rows inside subqueries count toward the 50,000 row limit. So keep subqueries filtered. Also, when a parent has very many children, loop over the child list with a for loop rather than copying it into a new list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with SOQL Relationship Queries<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Using <code>Course__c.Name<\/code> instead of <code>Course__r.Name<\/code>.<\/li>\n\n<li>Guessing the child relationship name instead of checking the field.<\/li>\n\n<li>Reading a parent field that was not selected, which throws an error.<\/li>\n\n<li>Forgetting that a parent lookup can be blank, which gives a null value.<\/li>\n\n<li>Returning thousands of children in a subquery without a filter.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for SOQL Relationship Queries<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Check relationship names in Object Manager before writing queries.<\/li>\n\n<li>Use child-to-parent dot notation to avoid extra queries in triggers.<\/li>\n\n<li>Filter subqueries with WHERE and limit them when possible.<\/li>\n\n<li>Check for null before reading parent fields from optional lookups.<\/li>\n\n<li>Use semi-joins and anti-joins instead of loops that compare lists.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Write a query on Contact that returns each student with the names of their active courses. Hint: use a subquery on the child relationship from Contact to Enrolment__c, then read <code>Course__r.Name<\/code> inside it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Interview Questions on SOQL Relationship Queries<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SOQL Relationship Queries are among the most tested topics in interviews. Here are the 20 questions asked most, with short answers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Questions on SOQL Relationship Queries<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: What is a relationship query?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It reads fields from related objects in one SOQL query, so you avoid running separate queries.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: What are the two types of relationship queries?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Child-to-parent, which uses dot notation, and parent-to-child, which uses a subquery.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: What does __r mean?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It marks a custom relationship name. For example, the field Course__c has the relationship name Course__r.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4: How do you read a standard parent field?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use the relationship name without a suffix. For example, Contact uses Account.Name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5: How many levels can child-to-parent queries go?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Up to five levels, for example Course__r.Trainer__r.Manager.Name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6: Where do you find the child relationship name?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On the lookup or master-detail field in Object Manager, under Child Relationship Name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7: What does a subquery return in Apex?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A list of child records on each parent record, such as course.Enrolments__r.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code and Scenario Questions on SOQL Relationship Queries<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8: Write a query for accounts with their contacts.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, use SELECT Name, (SELECT LastName FROM Contacts) FROM Account.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9: How do you find accounts with no contacts?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use an anti-join, such as WHERE Id NOT IN (SELECT AccountId FROM Contact).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10: How do you find courses with active enrolments only?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use a semi-join, such as WHERE Id IN (SELECT Course__c FROM Enrolment__c WHERE Status__c = &#8216;Active&#8217;).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11: Can a subquery have its own WHERE and ORDER BY?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. A subquery can use WHERE, ORDER BY and LIMIT.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12: What happens if a lookup is blank?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The parent reference is null, so reading a field on it gives null. Check for null before using it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13: How do relationship queries help in triggers?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They let you read parent data for all records in one query, so you avoid queries inside loops.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q14: Do subquery rows count toward limits?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Child rows count toward the 50,000 row limit, so filter subqueries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced Questions on SOQL Relationship Queries<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q15: What is a polymorphic relationship?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a lookup that can point to different objects, such as WhatId on Task. You can use TYPEOF in SOQL to read fields by type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q16: Can you filter parents by a child field without a semi-join?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. To filter parents by children, use a semi-join. A subquery alone only returns children, it does not filter parents.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q17: Why might parent-to-child subqueries with many children cause problems?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Large child lists use heap and row limits. So filter them, or query children separately in batches.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q18: How do you query fields from a master-detail parent?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The same way as a lookup, with the relationship name and dot notation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q19: What is the difference between a semi-join and a subquery in SELECT?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A semi-join filters parent records in the WHERE clause. A subquery in SELECT returns child records.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q20: How would you reduce three queries into one?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use child-to-parent dot notation for parent fields and a subquery for children, so one query returns all three levels of data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.soql_sosl.meta\/soql_sosl\/sforce_api_calls_soql_relationships.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Relationship Queries (Salesforce Developers)<\/a><\/li>\n\n<li><a href=\"https:\/\/trailhead.salesforce.com\/content\/learn\/modules\/apex_database\/apex_database_soql\" target=\"_blank\" rel=\"noopener noreferrer\">Trailhead: Write SOQL Queries<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>SOQL Relationship Queries read related records in one query.<\/li>\n\n<li>Child-to-parent uses dot notation, such as Course__r.Name.<\/li>\n\n<li>Parent-to-child uses a subquery with the child relationship name.<\/li>\n\n<li>Semi-joins and anti-joins filter parents by their children.<\/li>\n\n<li>Check relationship names and filter subqueries to stay within limits.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Relationship queries return records. Sometimes you need totals instead. Next, read our guide to <a href=\"https:\/\/swapnilzambare.com\/blog\/soql-aggregate-functions-group-by\/\">SOQL aggregate functions and GROUP BY<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SOQL Relationship Queries let you read related records in one query, such as each enrolment with its course name, or each course with all&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1484,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[8,96],"tags":[98,26,91,7,97,105],"swapz_series":[],"class_list":["post-1487","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","category-salesforce-developer","tag-apex","tag-intermediate","tag-interview-questions","tag-salesforce","tag-salesforce-developer","tag-soql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SOQL Relationship Queries: Parent and Child<\/title>\n<meta name=\"description\" content=\"SOQL Relationship Queries explained simply: child-to-parent dot notation, parent-to-child subqueries, __r names, semi-joins and 20 interview Q&amp;A.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swapnilzambare.com\/blog\/soql-relationship-queries-parent-child\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SOQL Relationship Queries: Parent and Child\" \/>\n<meta property=\"og:description\" content=\"SOQL Relationship Queries explained simply: child-to-parent dot notation, parent-to-child subqueries, __r names, semi-joins and 20 interview Q&amp;A.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/soql-relationship-queries-parent-child\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-04T13:30:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-relationship-queries-parent-child-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Swapnil Zambare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Swapnil Zambare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-relationship-queries-parent-child\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-relationship-queries-parent-child\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/922ac0131aaa7088c78c2cb1700e0c9a\"},\"headline\":\"SOQL Relationship Queries: Parent and Child Records in Apex\",\"datePublished\":\"2026-09-04T13:30:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-relationship-queries-parent-child\\\/\"},\"wordCount\":1626,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-relationship-queries-parent-child\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/soql-relationship-queries-parent-child-featured.png\",\"keywords\":[\"Apex\",\"Intermediate\",\"Interview Questions\",\"Salesforce\",\"Salesforce Developer\",\"SOQL\"],\"articleSection\":[\"Salesforce\",\"Salesforce Developer\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-relationship-queries-parent-child\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-relationship-queries-parent-child\\\/\",\"name\":\"SOQL Relationship Queries: Parent and Child\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-relationship-queries-parent-child\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-relationship-queries-parent-child\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/soql-relationship-queries-parent-child-featured.png\",\"datePublished\":\"2026-09-04T13:30:00+00:00\",\"description\":\"SOQL Relationship Queries explained simply: child-to-parent dot notation, parent-to-child subqueries, __r names, semi-joins and 20 interview Q&A.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-relationship-queries-parent-child\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-relationship-queries-parent-child\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-relationship-queries-parent-child\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/soql-relationship-queries-parent-child-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/soql-relationship-queries-parent-child-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"SOQL Relationship Queries featured image: title card with an Apex code example\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-relationship-queries-parent-child\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SOQL Relationship Queries: Parent and Child Records in Apex\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"name\":\"Swapnil Zambare - Blog\",\"description\":\"Salesforce &amp; Tech Guides\",\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\",\"name\":\"Swapnil Zambare - Blog\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"width\":512,\"height\":512,\"caption\":\"Swapnil Zambare - Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/theSwapnilZambare\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/922ac0131aaa7088c78c2cb1700e0c9a\",\"name\":\"Swapnil Zambare\",\"sameAs\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\"],\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/author\\\/swapnil-zambare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SOQL Relationship Queries: Parent and Child","description":"SOQL Relationship Queries explained simply: child-to-parent dot notation, parent-to-child subqueries, __r names, semi-joins and 20 interview Q&A.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/swapnilzambare.com\/blog\/soql-relationship-queries-parent-child\/","og_locale":"en_US","og_type":"article","og_title":"SOQL Relationship Queries: Parent and Child","og_description":"SOQL Relationship Queries explained simply: child-to-parent dot notation, parent-to-child subqueries, __r names, semi-joins and 20 interview Q&A.","og_url":"https:\/\/swapnilzambare.com\/blog\/soql-relationship-queries-parent-child\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-09-04T13:30:00+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-relationship-queries-parent-child-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/soql-relationship-queries-parent-child\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/soql-relationship-queries-parent-child\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/922ac0131aaa7088c78c2cb1700e0c9a"},"headline":"SOQL Relationship Queries: Parent and Child Records in Apex","datePublished":"2026-09-04T13:30:00+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/soql-relationship-queries-parent-child\/"},"wordCount":1626,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/soql-relationship-queries-parent-child\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-relationship-queries-parent-child-featured.png","keywords":["Apex","Intermediate","Interview Questions","Salesforce","Salesforce Developer","SOQL"],"articleSection":["Salesforce","Salesforce Developer"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/soql-relationship-queries-parent-child\/","url":"https:\/\/swapnilzambare.com\/blog\/soql-relationship-queries-parent-child\/","name":"SOQL Relationship Queries: Parent and Child","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/soql-relationship-queries-parent-child\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/soql-relationship-queries-parent-child\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-relationship-queries-parent-child-featured.png","datePublished":"2026-09-04T13:30:00+00:00","description":"SOQL Relationship Queries explained simply: child-to-parent dot notation, parent-to-child subqueries, __r names, semi-joins and 20 interview Q&A.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/soql-relationship-queries-parent-child\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/soql-relationship-queries-parent-child\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/soql-relationship-queries-parent-child\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-relationship-queries-parent-child-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-relationship-queries-parent-child-featured.png","width":2400,"height":1260,"caption":"SOQL Relationship Queries featured image: title card with an Apex code example"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/soql-relationship-queries-parent-child\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"SOQL Relationship Queries: Parent and Child Records in Apex"}]},{"@type":"WebSite","@id":"https:\/\/swapnilzambare.com\/blog\/#website","url":"https:\/\/swapnilzambare.com\/blog\/","name":"Swapnil Zambare - Blog","description":"Salesforce &amp; Tech Guides","publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swapnilzambare.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/swapnilzambare.com\/blog\/#organization","name":"Swapnil Zambare - Blog","url":"https:\/\/swapnilzambare.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","width":512,"height":512,"caption":"Swapnil Zambare - Blog"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/theSwapnilZambare"]},{"@type":"Person","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/922ac0131aaa7088c78c2cb1700e0c9a","name":"Swapnil Zambare","sameAs":["https:\/\/swapnilzambare.com\/blog"],"url":"https:\/\/swapnilzambare.com\/blog\/author\/swapnil-zambare\/"}]}},"_links":{"self":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1487","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/comments?post=1487"}],"version-history":[{"count":0,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1487\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/1484"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=1487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=1487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=1487"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=1487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}