title: Joining more than 2 Tables In Spark SQL
body: <p>I am trying to write a query in SPARK SQL performing join of three tables. But the query output is actually <code>null</code>. It is working fine for single table. My Join query is correct as I have already executed it in oracle database. What correction do I need to appply here? Spark version is 2.0.0.</p>
  <pre><code>from pyspark.sql import SQLContext, Row
  sqlContext = SQLContext(sc)
  
  lines = sc.textFile(&quot;/Users/Hadoop_IPFile/purchase&quot;)
  lines2 = sc.textFile(&quot;/Users/Hadoop_IPFile/customer&quot;)
  lines3 = sc.textFile(&quot;/Users/Hadoop_IPFile/book&quot;)
  
  parts = lines.map(lambda l: l.split(&quot;\t&quot;))
  purchase = parts.map(lambda p: Row(year=p[0],cid=p[1],isbn=p[2],seller=p[3],price=int(p[4])))
  schemapurchase = sqlContext.createDataFrame(purchase)
  schemapurchase.registerTempTable(&quot;purchase&quot;)
  
  
  parts2 = lines.map(lambda l: l.split(&quot;\t&quot;))
  customer = parts2.map(lambda p: Row(cid=p[0],name=p[1],age=p[2],city=p[3],sex=p[4]))
  schemacustomer = sqlContext.createDataFrame(customer)
  schemacustomer.registerTempTable(&quot;customer&quot;)
  
  parts3 = lines.map(lambda l: l.split(&quot;\t&quot;))
  book = parts3.map(lambda p: Row(isbn=p[0],name=p[1]))
  schemabook = sqlContext.createDataFrame(book)
  schemabook.registerTempTable(&quot;book&quot;)
  
  result_purchase = sqlContext.sql(&quot;&quot;&quot;SELECT DISTINCT customer.name AS name FROM purchase JOIN book ON purchase.isbn = book.isbn JOIN customer ON customer.cid = purchase.cid WHERE customer.name != 'Harry Smith' AND purchase.isbn IN (SELECT purchase.isbn FROM customer JOIN purchase ON customer.cid = purchase.cid WHERE customer.name = 'Harry Smith')&quot;&quot;&quot;)
  
  result = result_purchase.rdd.map(lambda p: &quot;name: &quot; + p.name).collect()
  for name in result:
      print(name)
  
  
  DataSet
  ---------
  Purchase
  1999    C1  B1  Amazon  90
  2001    C1  B2  Amazon  20
  2008    C2  B2  Barnes Noble    30
  2008    C3  B3  Amazon  28
  2009    C2  B1  Borders 90
  2010    C4  B3  Barnes Noble    26
  
  
  Customer
  C1  Jackie Chan 50  Dayton  M
  C2  Harry Smith 30  Beavercreek M
  C3  Ellen Smith 28  Beavercreek F
  C4  John Chan   20  Dayton  M
  
  Book
  B1  Novel
  B2  Drama
  B3  Poem
  </code></pre>
  <p>I found below instruction in some webpage, but it is still not working:</p>
  <pre><code>schemapurchase.join(schemabook, schemapurchase.isbn == schemabook.isbn) 
  schemapurchase.join(schemacustomer, schemapurchase.cid == schemacustomer.cid)
  </code></pre>