Tech Junkie Blog - Real World Tutorials, Happy Coding!: SQL: RIGHT JOIN

Thursday, October 10, 2019

SQL: RIGHT JOIN

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.ProductName
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

Here are the results:


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.ProductName
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
Here are the results:



3 comments:

  1. I want to show my thanks to the writer just for rescuing me from this matter. After exploring through the online world and meeting basics which were not powerful, I figured my entire life was done. Living without the approaches to the issues you have fixed all through your report is a critical case, and the ones that would have adversely affected my career if I had not discovered your website. Your good natural talent and kindness in playing with a lot of things was tremendous. I am not sure what I would've done if I hadn't encountered such a point like this. It’s possible to at this point look ahead to my future. Thanks so much for your reliable and amazing guide. I will not think twice to recommend the sites to anyone who would need support on this issue.Refer best essay writing service to get any help in writing.

    ReplyDelete
  2. Nice Information
    We are the best piping design course in Hyderabad, India. Sanjary academy Offers Piping Design Course and Best Piping Design Training Institute in Hyderabad. Piping Design Institute in India Piping Design Engineering.
    Piping Design Course
    Piping Design Course in india
    Piping Design Course in hyderabad

    ReplyDelete
  3. Good Post
    Sanjary kids is the best playschool, preschool in Hyderabad, India. Start your play school,preschool in Hyderabad with sanjary kids. Sanjary kids provides programs like Play group,Nursery,Junior KG,Serior KG,and Teacher Training Program.
    best preschool in hyderabad
    preschool teacher training
    playschools in hyderabad
    preschool teacher training in hyderabad

    ReplyDelete

Search This Blog