SQL Join syntax delayed ON operator

I learned something new today. In JOIN T-SQL syntax, the ON is not necessary immediately.   I’m not sure what benefit this would give, but is a nice piece of trivia.

For example, the following query WORKS in the AdventureWorks2012 database:

SELECT *
FROM HumanResources.EmployeePayHistory
INNER JOIN HumanResources.Employee
INNER JOIN HumanResources.EmployeeDepartmentHistory
INNER JOIN HumanResources.Department

ON Department.DepartmentID =
   EmployeeDepartmentHistory.DepartmentID

ON EmployeeDepartmentHistory.BusinessEntityID =
   Employee.BusinessEntityID

ON EmployeePayHistory.BusinessEntityID =
   Employee.BusinessEntityID

The order of the ONs matters.   The last two tables joined need to have the first ON, continuing in reverse order.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s