Question

Here is the deal, I have this SQL query

SELECT *
FROM Customers AS C, Orders AS O, Orderlines AS OL
WHERE C.CustomerID = O.CustomerID AND O .OrderID = OL.OrderID

which in relational algebra is

Σ(C.CustomerID=O.CustomerID AND O.OrderID=OL.OrderID)(Customers x Orders x Orderlines) .

How can I "convert" this 3-way Cartesian product into a Join relation ?

Thanks a lot in advance.

P.S Please correct me if I am wrong about the relational algebra part.

Was it helpful?

Solution

Seems pretty straight forward...

SELECT c.*, o.*, ol.*
FROM Customers c
INNER JOIN Orders o ON c.CustomerId = o.CustomerID
INNER JOIN OrderLines ol ON o.OrderId = ol.OrderId

I'd advise against selecting * from every table though, especially given the duplicated column names.

OTHER TIPS

SELECT *
FROM Customers AS C
     JOIN Orders AS O ON C.CustomerID = O.CustomerID
     JOIN Orderlines AS OL ON O.OrderID = OL.OrderID
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top