{"id":3951,"date":"2025-06-07T08:09:17","date_gmt":"2025-06-07T08:09:17","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3951"},"modified":"2025-06-07T09:58:46","modified_gmt":"2025-06-07T09:58:46","slug":"sql-right-join","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-right-join\/","title":{"rendered":"SQL RIGHT JOIN"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>RIGHT JOIN<\/code> returns <strong>all rows from the right table<\/strong>, and matched rows from the left. If no match exists, left-side columns return <code>NULL<\/code>.<\/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 columns\nFROM table1\nRIGHT JOIN table2\n  ON table1.common_column = table2.common_column;<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Example: List All Departments and Their Employees<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT\n  e.employee_name,\n  d.department_name\nFROM employees e\nRIGHT JOIN departments d\n  ON e.department_id = d.department_id;<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 All departments are shown \u2014 even those with no employees (in that case, <code>employee_name<\/code> is <code>NULL<\/code>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Use Case: Find Departments Without Employees<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT d.department_name\nFROM employees e\nRIGHT JOIN departments d\n  ON e.department_id = d.department_id\nWHERE e.employee_id IS NULL;<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd0d This identifies departments that currently have no employees.<\/p>\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>RIGHT JOIN<\/code><\/td><td>Keeps all records from right table<\/td><\/tr><tr><td>No match?<\/td><td>Left table columns return <code>NULL<\/code><\/td><\/tr><tr><td>Useful for<\/td><td>Highlighting unmatched records in the right table<\/td><\/tr><tr><td>Similar to<\/td><td><code>LEFT JOIN<\/code>, but direction is reversed<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u27a1\ufe0f Use <code>RIGHT JOIN<\/code> when <strong>right table data must be fully preserved<\/strong><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>RIGHT JOIN returns all rows from the right table, and matched rows from the left. If no match exists, left-side columns return NULL. \ud83d\udd39 Basic Syntax \ud83d\udd39 Example: List All Departments and Their Employees \u2705 All departments are shown \u2014 even those with no employees (in that case, employee_name is NULL). \ud83d\udd39 Use Case: Find [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3951","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3951","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=3951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}