{"id":155,"date":"2012-09-17T09:58:08","date_gmt":"2012-09-17T04:58:08","guid":{"rendered":"http:\/\/learnsf.wordpress.com\/?p=155"},"modified":"2024-01-01T12:34:23","modified_gmt":"2024-01-01T18:34:23","slug":"exists-clause-in-soql","status":"publish","type":"post","link":"https:\/\/blog.felineflock.com\/index.php\/2012\/09\/17\/exists-clause-in-soql\/","title":{"rendered":"How to emulate EXISTS clause in SOQL"},"content":{"rendered":"<p>Here is how to emulate the EXISTS clause in SOQL<\/p>\n<p>In the example below Education__c is a custom object that has a lookup field to Contact.<\/p>\n<p>Suppose you have a query that would be written like this:<\/p>\n<blockquote><p>SELECT Id, Name<br \/>\nFROM Contact cn<br \/>\nWHERE EXISTS (<\/p>\n<p style=\"padding-left: 90px;\">SELECT 1<br \/>\nFROM Education__c<br \/>\nWHERE Contact__c = \u00a0cn.Id )<\/p>\n<\/blockquote>\n<p>It can be rewritten in SOQL using an Id Semi-Join as:<\/p>\n<blockquote><p>SELECT Id, Name<br \/>\nFROM Contact cn<br \/>\nWHERE Id IN (<\/p>\n<p style=\"padding-left: 90px;\">SELECT Contact__c<\/p>\n<p style=\"padding-left: 90px;\">FROM Education__c )<\/p>\n<\/blockquote>\n<p>It will retrieve all Contacts that have an associated Education__c record.<\/p>\n<p>It is possible to use up to 2 Id Semi-Joins in a SOQL query (<a href=\"http:\/\/www.salesforce.com\/us\/developer\/docs\/api\/Content\/sforce_api_calls_soql_select_comparisonoperators.htm\" target=\"_blank\" rel=\"noopener\">more information about it<\/a>).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is how to emulate the EXISTS clause in SOQL In the example below Education__c is a custom object that has a lookup field to Contact. Suppose you have a query that would be written like this: SELECT Id, Name FROM Contact cn WHERE EXISTS ( SELECT 1 FROM Education__c WHERE Contact__c = \u00a0cn.Id ) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[13],"tags":[22],"class_list":["post-155","post","type-post","status-publish","format-standard","hentry","category-no-category","tag-soql"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts\/155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/comments?post=155"}],"version-history":[{"count":2,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts\/155\/revisions"}],"predecessor-version":[{"id":594,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts\/155\/revisions\/594"}],"wp:attachment":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/media?parent=155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/categories?post=155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/tags?post=155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}