{"id":3955,"date":"2025-06-07T08:13:38","date_gmt":"2025-06-07T08:13:38","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3955"},"modified":"2025-06-07T09:59:08","modified_gmt":"2025-06-07T09:59:08","slug":"sql-self-join","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-self-join\/","title":{"rendered":"SQL SELF JOIN"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A <strong>SELF JOIN<\/strong> is a regular <code>JOIN<\/code> where a table is joined <strong>with itself<\/strong> to compare rows within the same table.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It\u2019s commonly used for <strong>hierarchical data<\/strong>, like employees and managers, or products and related products.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Basic Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT A.column1, B.column2\nFROM table_name A\nJOIN table_name B\n  ON A.common_column = B.common_column;<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Example: Employees and Their Managers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Assume each employee has a <code>manager_id<\/code> referring to another employee&#8217;s <code>employee_id<\/code> in the same table.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT\n  e.employee_name AS employee,\n  m.employee_name AS manager\nFROM employees e\nLEFT JOIN employees m\n  ON e.manager_id = m.employee_id;<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 This shows each employee and their manager&#8217;s name.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 When to Use SELF JOIN<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Compare rows in the same table<\/li>\n\n\n\n<li>Analyze hierarchical or paired data<\/li>\n\n\n\n<li>Find duplicate or related records<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 Quick Recap<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Key Point<\/th><th>Explanation<\/th><\/tr><\/thead><tbody><tr><td><code>SELF JOIN<\/code><\/td><td>A table joins with itself<\/td><\/tr><tr><td>Use case<\/td><td>Hierarchies, comparisons, duplicates<\/td><\/tr><tr><td>Aliases<\/td><td>Required to distinguish the same table used twice<\/td><\/tr><tr><td>JOIN type<\/td><td>Can be <code>INNER<\/code>, <code>LEFT<\/code>, etc., depending on the need<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd01 Use SELF JOIN for comparing or relating data <strong>within a single table<\/strong><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A SELF JOIN is a regular JOIN where a table is joined with itself to compare rows within the same table. It\u2019s commonly used for hierarchical data, like employees and managers, or products and related products. \ud83d\udd39 Basic Syntax \ud83d\udd39 Example: Employees and Their Managers Assume each employee has a manager_id referring to another employee&#8217;s [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3955","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3955","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/cposts"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=3955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}