<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dean Wright</title>
    <description>The latest articles on DEV Community by Dean Wright (@deanwright_sql).</description>
    <link>https://dev.to/deanwright_sql</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4259568%2F216b54fa-f9d7-49e0-a732-2a0ec1b9cd7b.png</url>
      <title>DEV Community: Dean Wright</title>
      <link>https://dev.to/deanwright_sql</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/deanwright_sql"/>
    <language>en</language>
    <item>
      <title>SQL JOINs, one table at a time</title>
      <dc:creator>Dean Wright</dc:creator>
      <pubDate>Mon, 23 Nov 2026 02:30:44 +0000</pubDate>
      <link>https://dev.to/deanwright_sql/sql-joins-one-table-at-a-time-4ohm</link>
      <guid>https://dev.to/deanwright_sql/sql-joins-one-table-at-a-time-4ohm</guid>
      <description>&lt;p&gt;Most JOIN tutorials start with a Venn diagram. That picture is fine for remembering the difference between INNER and LEFT, but it is a poor way to write a query, because a JOIN is not a Venn diagram. It is a rule for pairing rows, followed by a rule for what to do with rows that do not pair.&lt;/p&gt;

&lt;p&gt;If you keep those two steps separate, JOINs stop feeling like a special syntax and start feeling like the rest of SQL.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start with two ordinary tables
&lt;/h2&gt;

&lt;p&gt;Imagine a small shop. One table holds customers, the other holds orders.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- customers&lt;/span&gt;
&lt;span class="c1"&gt;-- id | name&lt;/span&gt;
&lt;span class="c1"&gt;-- 1  | Ava&lt;/span&gt;
&lt;span class="c1"&gt;-- 2  | Ben&lt;/span&gt;
&lt;span class="c1"&gt;-- 3  | Chen&lt;/span&gt;

&lt;span class="c1"&gt;-- orders&lt;/span&gt;
&lt;span class="c1"&gt;-- id | customer_id | total&lt;/span&gt;
&lt;span class="c1"&gt;-- 10 | 1           | 24.00&lt;/span&gt;
&lt;span class="c1"&gt;-- 11 | 1           | 18.50&lt;/span&gt;
&lt;span class="c1"&gt;-- 12 | 2           | 9.00&lt;/span&gt;
&lt;span class="c1"&gt;-- 13 | 4           | 40.00   -- customer 4 is not in customers&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The question "which orders belong to which customer?" is a pairing rule: &lt;code&gt;orders.customer_id = customers.id&lt;/code&gt;. Everything else is a decision about unmatched rows.&lt;/p&gt;

&lt;h2&gt;
  
  
  INNER JOIN: keep only the pairs
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;INNER&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;
  &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;SQL finds every pair of rows that satisfies the &lt;code&gt;ON&lt;/code&gt; clause and throws the rest away. Ava appears twice, because she has two orders. Chen disappears, because she has none. Order 13 disappears, because customer 4 is missing.&lt;/p&gt;

&lt;p&gt;That disappearance is the usual surprise. An INNER JOIN is not "all customers and their orders." It is "only the combinations that match." If you needed Chen in the result, you chose the wrong join.&lt;/p&gt;

&lt;h2&gt;
  
  
  LEFT JOIN: keep the left table intact
&lt;/h2&gt;

&lt;p&gt;A LEFT JOIN uses the same pairing rule, then changes the second step. Every row from the left table survives. If it has no match, the right-hand columns come back as NULL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;
  &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ava still appears twice. Ben appears once. Chen appears once, with &lt;code&gt;order_id&lt;/code&gt; and &lt;code&gt;total&lt;/code&gt; set to NULL. That NULL is not a missing value in the customers table; it is the join telling you "no pair was found."&lt;/p&gt;

&lt;p&gt;This is the pattern you want for questions such as "list every customer, and their orders if they have any" or "which customers have never ordered?" The second question is just a LEFT JOIN plus a filter on the NULL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;
  &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Filter on a right-table column in &lt;code&gt;WHERE&lt;/code&gt; after a LEFT JOIN and you have, in effect, turned it back into an INNER JOIN. If you meant to keep unmatched left rows, that test belongs in the &lt;code&gt;ON&lt;/code&gt; clause, or you should be checking &lt;code&gt;IS NULL&lt;/code&gt; on purpose.&lt;/p&gt;

&lt;h2&gt;
  
  
  RIGHT JOIN and FULL JOIN
&lt;/h2&gt;

&lt;p&gt;A RIGHT JOIN is a LEFT JOIN with the tables swapped. Most people write LEFT JOIN consistently so the "preserved" table is always on the left. A FULL JOIN keeps unmatched rows from both sides: Chen appears with NULL order columns, and order 13 appears with NULL customer columns. Full joins are less common in day-to-day analyst work, but they are the right tool when you are reconciling two lists that should be complete and might not be.&lt;/p&gt;

&lt;h2&gt;
  
  
  CROSS JOIN, briefly
&lt;/h2&gt;

&lt;p&gt;A CROSS JOIN has no &lt;code&gt;ON&lt;/code&gt; clause. It pairs every row on the left with every row on the right. Three customers and four orders would produce twelve rows. That is sometimes useful (a date spine against a list of shops, for example). It is also how a forgotten join condition quietly explodes a result set. If a query suddenly returns far more rows than either table, look for a missing or too-loose &lt;code&gt;ON&lt;/code&gt; clause before you look for a complicated bug.&lt;/p&gt;

&lt;h2&gt;
  
  
  A working habit
&lt;/h2&gt;

&lt;p&gt;When I write a join, I do it in this order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Write the &lt;code&gt;FROM&lt;/code&gt; table — the grain I want to preserve. If I must not lose customers, customers goes first.&lt;/li&gt;
&lt;li&gt;Add one join at a time, with an &lt;code&gt;ON&lt;/code&gt; clause I can say out loud: "an order belongs to a customer when &lt;code&gt;orders.customer_id = customers.id&lt;/code&gt;."&lt;/li&gt;
&lt;li&gt;Choose INNER or LEFT based on whether unmatched rows should vanish or survive as NULLs.&lt;/li&gt;
&lt;li&gt;Only then add &lt;code&gt;WHERE&lt;/code&gt;, &lt;code&gt;GROUP BY&lt;/code&gt;, and &lt;code&gt;SELECT&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you join three or four tables at once and the result looks wrong, comment all but one join back out and re-add them in that same order. The mistake is almost always in a single pairing rule, or in a &lt;code&gt;WHERE&lt;/code&gt; clause that quietly discarded the NULLs you were counting on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practice, not diagrams
&lt;/h2&gt;

&lt;p&gt;You will not remember this from a diagram. You will remember it after you have written the same inner and left joins enough times that the unmatched-row behaviour feels obvious. A short loop works well: pick two tables you understand, write the inner join, write the left join, then write the "who has no match?" query. If you want a free course with in-browser exercises so you can drill those patterns against a real database, I keep a regularly updated comparison of genuinely free SQL courses at &lt;a href="https://freesqlcourses.com" rel="noopener noreferrer"&gt;freesqlcourses.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The Venn diagram can stay in your notes. The pairing rule, and what you do with the leftovers, is what you actually run.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
