{"id":3949,"date":"2025-06-07T08:06:58","date_gmt":"2025-06-07T08:06:58","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3949"},"modified":"2025-06-07T09:58:31","modified_gmt":"2025-06-07T09:58:31","slug":"sql-left-join","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-left-join\/","title":{"rendered":"SQL LEFT JOIN"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>LEFT JOIN<\/code> returns <strong>all rows from the left table<\/strong>, and matched rows from the right. If there\u2019s <strong>no match<\/strong>, right-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\nLEFT 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 Employees with Their Departments<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT\n  e.employee_name,\n  d.department_name\nFROM employees e\nLEFT JOIN departments d\n  ON e.department_id = d.department_id;<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Employees with no department still appear, but <code>department_name<\/code> will be <code>NULL<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Use Case: Identify Unlinked Records<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT e.name\nFROM employees e\nLEFT JOIN departments d\n  ON e.department_id = d.id\nWHERE d.id IS NULL;<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udfe1 This finds employees not assigned to any department.<\/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>LEFT JOIN<\/code><\/td><td>Keeps all records from left table<\/td><\/tr><tr><td>No match?<\/td><td>Right table columns return <code>NULL<\/code><\/td><\/tr><tr><td>Helpful for<\/td><td>Finding missing or optional related data<\/td><\/tr><tr><td>Use with<\/td><td><code>WHERE<\/code> to filter unmatched records (<code>IS NULL<\/code>)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83e\udde9 Use <code>LEFT JOIN<\/code> when the relationship is <strong>optional<\/strong><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>LEFT JOIN returns all rows from the left table, and matched rows from the right. If there\u2019s no match, right-side columns return NULL. \ud83d\udd39 Basic Syntax \ud83d\udd39 Example: List All Employees with Their Departments \u2705 Employees with no department still appear, but department_name will be NULL. \ud83d\udd39 Use Case: Identify Unlinked Records \ud83d\udfe1 This finds [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3949","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3949","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=3949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}