RIGHT JOIN works like the INNER JOIN, it just returns all the records that are on the right side of the = sign on the RIGHT JOIN clause. For example let's say you want to get a record of all customers who orders a certain product.
You will use the RIGHT JOIN by query all the orders in the Orders table then linking it to the OrderDetails table and then eventually linking it to the Products table.
SELECT c.CompanyName,c.ContactName,c.ContactTitle,od.OrderID,p.ProductID,p.ProductNameHere are the results:
FROM Customers c
RIGHT JOIN Orders o ON o.CustomerID = c.CustomerID
RIGHT JOIN [Order Details] od ON o.OrderID = od.OrderID
RIGHT JOIN Products p ON p.ProductID = od.ProductID
You can filter the results further by adding a WHERE claus for a specific product id.
SELECT c.CompanyName,c.ContactName,c.ContactTitle,od.OrderID,p.ProductID,p.ProductNameHere are the results:
FROM Customers c
RIGHT JOIN Orders o ON o.CustomerID = c.CustomerID
RIGHT JOIN [Order Details] od ON o.OrderID = od.OrderID
RIGHT JOIN Products p ON p.ProductID = od.ProductID
WHERE p.ProductID = 33
No comments:
Post a Comment