title: Cleanest, most efficient syntax to perform DataFrame self-join in Spark
body: <p>In standard SQL, when you join a table to itself, you can create aliases for the tables to keep track of which columns you are referring to:</p>
  
  <pre><code>SELECT a.column_name, b.column_name...
  FROM table1 a, table1 b
  WHERE a.common_field = b.common_field;
  </code></pre>
  
  <p>There are two ways I can think of to achieve the same thing using the Spark <code>DataFrame</code> API:</p>
  
  <p><strong>Solution #1: Rename the columns</strong></p>
  
  <p>There are a couple of different methods for this in answer to <strong><a href="https://stackoverflow.com/questions/36206590/how-to-do-select-with-column-name-that-appears-twice-in-dataframe">this question</a></strong>. This one just renames all the columns with a specific suffix:</p>
  
  <pre><code>df.toDF(df.columns.map(_ + "_R"):_*)
  </code></pre>
  
  <p>For example you can do:</p>
  
  <pre><code>df.join(df.toDF(df.columns.map(_ + "_R"):_*), $"common_field" === $"common_field_R")
  </code></pre>
  
  <p><strong>Solution #2: Copy the reference to the <code>DataFrame</code></strong></p>
  
  <p>Another simple solution is to just do this:</p>
  
  <pre><code>val df: DataFrame = ....
  val df_right = df
  
  df.join(df_right, df("common_field") === df_right("common_field"))
  </code></pre>
  
  <p>Both of these solutions work, and I could see each being useful in certain situations. Are there any internal differences between the two I should be aware of?</p>