<?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 LEE</title>
    <description>The latest articles on DEV Community by DEAN LEE (@dean_lee).</description>
    <link>https://dev.to/dean_lee</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3523909%2F137f7642-0459-4595-a389-5504d18bf4a3.jpeg</url>
      <title>DEV Community: DEAN LEE</title>
      <link>https://dev.to/dean_lee</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dean_lee"/>
    <language>en</language>
    <item>
      <title>"Table 'test.p' doesn't exist" — Understanding SQL Aliases and Default JOIN Behavior</title>
      <dc:creator>DEAN LEE</dc:creator>
      <pubDate>Fri, 03 Apr 2026 13:26:22 +0000</pubDate>
      <link>https://dev.to/dean_lee/table-testp-doesnt-exist-understanding-sql-aliases-and-default-join-behavior-1ndc</link>
      <guid>https://dev.to/dean_lee/table-testp-doesnt-exist-understanding-sql-aliases-and-default-join-behavior-1ndc</guid>
      <description>&lt;p&gt;While solving the LeetCode #175 (Combine Two Tables) problem, I encountered a couple of unexpected errors. &lt;/p&gt;

&lt;p&gt;What seemed like a simple query taught me a valuable lesson about SQL syntax and the fundamental behavior of JOINs. Here’s a breakdown of the "traps" I fell into and how I fixed them. &lt;/p&gt;




&lt;h2&gt;
  
  
  1. The Problem
&lt;/h2&gt;

&lt;p&gt;I initially wrote the following MySQL query to fetch person details along with their addresses:&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;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;state&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;P&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt; 
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;Address&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; 
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;P&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;personId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;personId&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, I hit a Runtime Error: Table 'test.p' doesn't exist. Even after fixing the syntax, I realized the logic was slightly off for the problem's requirements.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Root Cause Analysis
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Error 1: Incorrect Table Aliasing
&lt;/h3&gt;

&lt;p&gt;In SQL, the correct order for aliasing is [Table Name] [Alias].&lt;/p&gt;

&lt;p&gt;What I wrote: FROM P Person&lt;/p&gt;

&lt;p&gt;What happened: The SQL engine interpreted P as the table name and Person as the alias. Since there is no table named P in the database, it threw the "Table doesn't exist" error.&lt;/p&gt;

&lt;h3&gt;
  
  
  Error 2: The "Default JOIN" Misconception
&lt;/h3&gt;

&lt;p&gt;I used to think that a plain JOIN would behave like a LEFT JOIN by default. I was wrong. &lt;strong&gt;In standard SQL (MySQL, PostgreSQL, etc.), JOIN is shorthand for INNER JOIN.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Inner Join: Returns rows only when there is a match in both tables (the intersection).&lt;/p&gt;

&lt;p&gt;The Issue: The problem asked to report null if the address is missing. An INNER JOIN would simply drop those people from the results entirely.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. The Solution: Switching to LEFT JOIN
&lt;/h2&gt;

&lt;p&gt;To ensure that every person in the Person table is reported (even those without an address), we must use a LEFT JOIN. This keeps all rows from the "left" table and fills in NULL for missing values from the "right" table.&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;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;state&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="n"&gt;p&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;Address&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; 
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;personId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;personId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  4. Key Takeaways
&lt;/h2&gt;

&lt;p&gt;Alias Order Matters: Always use Table p, not p Table.&lt;/p&gt;

&lt;p&gt;Be Explicit with JOINs: Never assume the default. If you need to preserve data from one side, explicitly use LEFT JOIN or RIGHT JOIN.&lt;/p&gt;

&lt;p&gt;Read the Requirements: If a problem mentions "report null if not present," it's a massive hint to use an Outer Join.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;It’s easy to overlook the basics when you're focused on complex logic. This "simple" LeetCode problem was a great reminder that understanding the underlying mechanics of SQL is just as important as getting the right output.&lt;/p&gt;

&lt;p&gt;Have you ever made a similar "default behavior" assumption in your code? Let me know in the comments!&lt;/p&gt;

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