title: T-sql multiple joins
body: <pre><code>INSERT INTO [dbo].[TestFactSales]
  ( [SalesOrderID]
  , [SalesOrderDetailID]
  , [OrderDateKey] -- Surrogate Key
  , [ShipDateKey] -- Surrogate Key
  , [CustomerKey] -- Surrogate Key
  , [ProductKey] -- Surrogate Key
  , [OrderQty]
  , [UnitPrice]
  , [UnitPriceDiscount]
  ) 
  SELECT 
    T2.SalesOrderID
  , T1.SalesOrderDetailID
  --, T2.OrderDate
  , T6.CalendarDateKey
  --, T2.ShipDate
  , T5.CalendarDateKey
  --, T2.CustomerID
  , T4.CustomerKey
  --, T1.ProductID
  , T3.ProductKey
  , T1.OrderQty
  , T1.UnitPrice
  , T1.UnitPriceDiscount
  FROM  [AdventureWorksLT2012].SalesLT.SalesOrderDetail as T1
  
  JOIN [AdventureWorksLT2012].SalesLT.SalesOrderHeader as T2
    ON T1.SalesOrderID = T2.SalesOrderID
  
  JOIN Tempdb.dbo.TestDimProducts as T3
   ON T1.ProductID = T3.ProductID
  
  JOIN TempDB.dbo.TestDimCustomers as T4
   ON T2.CustomerID = T4.CustomerID
  
  JOIN TempDB.dbo.TestDimDates as T5
   ON Cast(T2.ShipDate as date) = Cast(T5.CalendarDate as date)
  
  JOIN TempDB.dbo.TestDimDates as T6
   ON Cast(T2.OrderDate as date) = Cast(T6.CalendarDate as date)
  
  ;
  go
  </code></pre>
  
  <p>Hello,
  I have a problem about understating the code above. This is t-sql statement for very common join operations in data warehouse implementation. My problem is that when we join two tables with: </p>
  
  <pre><code>FROM  [AdventureWorksLT2012].SalesLT.SalesOrderDetail as T1
  JOIN [AdventureWorksLT2012].SalesLT.SalesOrderHeader as T2
   ON T1.SalesOrderID = T2.SalesOrderID
  </code></pre>
  
  <p>Does it mean that join tables T1 and T2 on T2 ? As in select statement, we have</p>
  
  <pre><code>Select
  T2.SalesOrderID
  , T1.SalesOrderDetailID
  </code></pre>
  
  <p>Hence how this select understands which table to look at,T1 or T2?
  Can you please clarify me about this issue? Please note that I understand the alias representation. What I dont understand is insert part. To insert [SalesOrderID], we need to take a look at joined table coming from T1 and T2. but the code looks T2.SalesOrderID.</p>