Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement SQL joins without using JOIN?

Tags:

sql

join

How does one implement SQL joins without using the JOIN keyword?

This is not really necessary, but I thought that by doing this I could better understand what joins actually do.

like image 815
giladrv Avatar asked Aug 06 '14 13:08

giladrv


People also ask

How can I join two tables without joining condition in SQL?

One way to join two tables without a common column is to use an obsolete syntax for joining tables. With this syntax, we simply list the tables that we want to join in the FROM clause then use a WHERE clause to add joining conditions if necessary.

Can we write SQL queries without using join operation if so how?

Yes, it is possible to join two tables without using the join keyword. Here is how you can do it. The result of the above query will be cross join between the two tables which are mentioned in the query.

Which join we can use without a join condition?

We can use 'cross join' without on condition. Cross join gives the result in cartesian product form. For instance, if in one table there are 3 records and another table has 2 records, then the first record will match with all the second table records.

How would you join two tables without using the joiner transformation?

Lets see one scenario, how to join two tables without using Joiner Transformation. This can be achieved by just using Source Qualifier – 'User Defined Join' property. Here we are going to join Two tables (EMPLOYEE & JOBS) and create a target table with columns from both the tables.


2 Answers

The basic INNER JOIN is easy to implement. The following:

SELECT L.XCol, R.YCol
FROM LeftTable AS L
INNER JOIN RightTable AS R
ON L.IDCol=R.IDCol;

is equivalent to:

SELECT L.XCol, R.YCol
FROM LeftTable AS L, RightTable AS R
WHERE L.IDCol=R.IDCol;

In order to extend this to a LEFT/RIGHT/FULL OUTER JOIN, you only need to UNION the rows with no match, along with NULL in the correct columns, to the previous INNER JOIN.

For a LEFT OUTER JOIN, add:

UNION ALL
SELECT L.XCol, NULL /* cast the NULL as needed */
FROM LeftTable AS L
WHERE NOT EXISTS (
    SELECT * FROM RightTable AS R
    WHERE L.IDCol=R.IDCol)

For a RIGHT OUTER JOIN, add:

UNION ALL
SELECT NULL, R.YCol /* cast the NULL as needed */
FROM RightTable AS R
WHERE NOT EXISTS (
    SELECT * FROM LeftTable AS L
    WHERE L.IDCol=R.IDCol)

For a FULL OUTER JOIN, add both of the above.

like image 147
giladrv Avatar answered Sep 20 '22 17:09

giladrv


There is an older deprecated SQL syntax that allows you to join without using the JOIN keyword.. but I personally find it more confusing than any permutation of the JOIN operator I've ever seen. Here's an example:

SELECT A.CustomerName, B.Address1, B.City, B.State, B.Zip
FROM dbo.Customers A, dbo.Addresses B
WHERE A.CustomerId = B.CustomerId

In the older way of doing it, you join by separating the tables with a comma and specifying the JOIN conditions in the WHERE clause. Personally, I would prefer the JOIN syntax:

SELECT A.CustomerName, B.Address1, B.City, B.State, B.Zip
FROM dbo.Customers A
JOIN dbo.Addresses B
ON A.CustomerId = B.CustomerId

The reason you should shy away from this old style of join is clarity and readability. When you are simply joining one table to another, it's pretty easy to figure out what's going on. When you're combining multiple types of joins across a half dozen (or more) tables, this older syntax becomes very challenging to manage.

The best way to get a handle on the JOIN operator is working with it. Here's a decent visual example of what the different JOINs do:

http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/

Some more info:

https://sqlblog.org/2009/10/08/bad-habits-to-kick-using-old-style-joins

http://www.sqlservercentral.com/blogs/brian_kelley/2009/09/30/the-old-inner-join-syntax-vs-the-new-inner-join-syntax/

like image 22
Patrick Tucci Avatar answered Sep 17 '22 17:09

Patrick Tucci