Skip to content
Advertisement

SQL – How can you use WHERE instead of LEFT/RIGHT JOIN?

since I am a bit rusty, I was practicing SQL on this link and was trying to replace the LEFT JOIN completly with WHERE. How can i do this so it does the same thing as the premade function in the website?

What I tried so far is:

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers, Orders
WHERE Customers.CustomerID = Orders.CustomerID OR Customers.CustomerID != Orders.CustomerID
Order by Customers.CustomerName; 

Thanks in advance for your help.

Advertisement

Answer

You are trying to replace

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID

with

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers, Orders
WHERE ???

this is doomed to failure. Consider Customers has two rows and Orders has zero. The outer join will return two rows.

The cross join (FROM Customers, Orders) will return zero rows.

In standard SQL a WHERE clause can only reduce the rows from that – not increase them so there is nothing you can put for ??? that will give your desired results.

Before ANSI-92 joins were introduced some systems used to have proprietary operators for this, such as *= in SQL Server but this was removed from the product.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement