title: Why Redshift automatically trims varchar column when joining?
body: <p>I encountered unique problem when using Redshift. Please see the below illustrative example:</p>
  
  
  
  <pre class="lang-sql prettyprint-override"><code>drop table if exists joinTrim_temp1;
  create table joinTrim_temp1(rowIndex1 int, charToJoin1 varchar(20));
  insert into joinTrim_temp1 values(1, 'Sudan' );
  insert into joinTrim_temp1 values(2, 'Africa' );
  insert into joinTrim_temp1 values(3, 'USA' );
  
  drop table if exists joinTrim_temp2;
  create table joinTrim_temp2(rowIndex2 int, charToJoin2 varchar(20));
  insert into joinTrim_temp2 values(1, 'Sudan ' );
  insert into joinTrim_temp2 values(2, 'Africa ' );
  insert into joinTrim_temp2 values(3, 'USA ' );
  
  select * from joinTrim_temp1 a join joinTrim_temp2 b on a.charToJoin1 = b.charToJoin2;
  </code></pre>
  
  <p>The output of the query is as below:</p>
  
  <p><a href="https://i.stack.imgur.com/j4hB4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/j4hB4.png" alt="query Output"></a></p>
  
  <p>In the query you can see that there is a trailing space in the second table. So no inner join should take place. But it seems that Redshift is able to trim the trailing whitespaces when joining. </p>
  
  <p>I encountered this problem, while converting the existing Redshift sql code to PySpark.</p>
  
  <p>Regards,
  Kumar</p>