21

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.

giladrv
  • 1,024
  • 1
  • 9
  • 22
  • 3
    yikes, don't learn bad habits at the outset, try this instead: http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins – Paul Maxwell Aug 06 '14 at 13:43
  • @PaulMaxwell That article is poor. [Such Venn-like diagrams for joins are unclear, unhelpful & misleading.](https://stackoverflow.com/a/55642928/3404097) [See my Q&A comments here.](https://stackoverflow.com/q/38549/3404097) All you have to do to see this is try to write a correct legend for one. Don't forget that SQL tables are bags not sets. Joins are on any condition & don't need constraints so at best the diagrams when explained address special cases and/or partial properties. (Wow, as I was composing this someone posted yet another poor answer at the 2nd link.) – philipxy Mar 21 '22 at 02:12
  • Does this answer your question? [Explicit vs implicit SQL joins](https://stackoverflow.com/questions/44917/explicit-vs-implicit-sql-joins) – philipxy Mar 21 '22 at 04:07
  • @philipxy I disagree. For an initiate it can be very helpful to get a quick visualization. Of course the Venn/Euler diagrams are simplistic - but that is the very point of them. – Paul Maxwell Mar 21 '22 at 07:10
  • "All you have to do to see this is try to write a correct legend for one." – philipxy Mar 21 '22 at 07:36

3 Answers3

42

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.

giladrv
  • 1,024
  • 1
  • 9
  • 22
  • 1
    This works alternatively for join queries, so no need to use join queries, isn't it? Can this method be accepted as join queries? – Kavindu Gayantha Jul 06 '20 at 19:33
10

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/

Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
Patrick Tucci
  • 1,824
  • 1
  • 16
  • 22
2

When SQL was an infant we didn't have "inner join" "left outer join" etc. All we did was list the tables like this:

FROM table1, table2, table3, .... tablen

Then we had a where clause that was like a novel in length, some of the conditions were for filtering the data, many of the conditions were to join tables, like this

FROM table1, table2, table2, .... tablen
WHERE table1.code = 'x' and table1.id = table3.fk and table2.name like 'a%' and table2.id = table1.fk and tablen.fk = table3.id and table2.dt >= '2014-01-01'

from this we hoped like heck we had all the tables nicely related and we crossed our fingers. The worst case scenario - which happened a lot - was that we forgot to include a table at all in the where clause. This was not nice because what we get when we do that is a "Cartesian product" (basically a multiplication of all rows by the number of rows in the table we missed).

Then came ANSI standard join syntax, and life was better. We now place the join conditions on the join - not in the where clause - and as a bonus the where clause is easier to understand.

I don't think you will find it easier to understand this ancient syntax, for example an outer join was join = bizarre(+) or maybe it was (+)bizarre = join (I try not to remember).

Try http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins

Paul Maxwell
  • 33,002
  • 3
  • 32
  • 51