0

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.

1
  • Ah, relational algebra. Such fond memories
    – Phil
    Jan 30, 2014 at 22:40

2 Answers 2

2

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.

1
SELECT *
FROM Customers AS C
     JOIN Orders AS O ON C.CustomerID = O.CustomerID
     JOIN Orderlines AS OL ON O.OrderID = OL.OrderID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.