{"id":7402,"date":"2025-08-06T00:00:22","date_gmt":"2025-08-05T18:30:22","guid":{"rendered":"https:\/\/www.sapewmhelp.com\/?question=what-is-the-difference-between-association-vs-join-in-cds"},"modified":"2025-08-06T00:02:07","modified_gmt":"2025-08-05T18:32:07","slug":"what-is-the-difference-between-association-vs-joins-in-cds","status":"publish","type":"question","link":"https:\/\/www.sapewmhelp.com\/?question=what-is-the-difference-between-association-vs-joins-in-cds","title":{"rendered":"What is the difference between association vs Joins in CDS ?"},"content":{"rendered":"<p><strong>Association<\/strong> An association is a declarative relationship between CDS entities (like foreign keys), defined using the association keyword. <strong>Key Characteristics:<\/strong> Lazy\/Evaluated-on-demand: Data from the association is not fetched unless it is explicitly accessed using a path expression (like to_customer.name). Navigation-style access: You can access associated data using dot (.) notation. Defined once, reused many times: Associations can be defined once and used in multiple views or queries. Cardinality can be defined: e.g. [0..1], [1..*].<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>define view ZSalesOrder as select from sales_order\r\nassociation [0..1] to customer as _Customer on $projection.customer_id = _Customer.id\r\n{\r\nkey sales_order_id,\r\ncustomer_id,\r\n_Customer.name\r\n}\r\n<\/code><\/pre>\n<p>Here, _Customer is an association. It won&#8217;t trigger a join unless _Customer.name is accessed.<\/p><\/div>\n<div class=\"hcb_wrap\"><strong>Join<\/strong><\/div>\n<div class=\"hcb_wrap\">A join is an explicit operation in CDS to combine two data sources based on a condition, much like in SQL. <strong>Key Characteristics:<\/strong> Eager execution: The join happens immediately during view evaluation. More control: You control the type of join (INNER, LEFT OUTER, etc.). Typically used when data from both tables is always needed.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>define view ZSalesOrderJoin as select from sales_order\r\ninner join customer on sales_order.customer_id = customer.id\r\n{\r\nsales_order.id,\r\ncustomer.name\r\n}<\/code><\/pre>\n<p>Here, a real-time INNER JOIN is performed.<\/p><\/div>\n<h5><strong>When to Use What?<\/strong><\/h5>\n<p><strong>Use Association:<\/strong><\/p>\n<ul>\n<li>When modeling data relationships.<\/li>\n<li>When you want flexibility and better performance for optional fields.<\/li>\n<li>When the associated data isn\u2019t always needed.<\/li>\n<\/ul>\n<p><strong>Use Join:<\/strong><\/p>\n<ul>\n<li>When you always need data from both sources.<\/li>\n<li>When performing aggregations or filters based on joined fields.<\/li>\n<li>When doing complex multi-joins with specific types (e.g. inner join).<\/li>\n<\/ul>\n<\/div>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","question-category":[173],"question_tags":[178,306,199,208,305,177],"class_list":["post-7402","question","type-question","status-publish","hentry","question-category-cdsviews","question_tags-abap","question_tags-association","question_tags-cds","question_tags-hana","question_tags-joins","question_tags-sap"],"_links":{"self":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/question\/7402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/question"}],"about":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/types\/question"}],"author":[{"embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7402"}],"wp:attachment":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7402"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion-category&post=7402"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion_tags&post=7402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}