JOINS; LEFT, RRIGHT and INNER JOINS!

Joins in SQL helps to combine rows of different tables based on a common column.

Customer IDName
1Alice
2Bob
3Charlie
Order IDCustomer IDProduct
1014Laptop
1022Phone
1035Laptop

INNER JOIN returns matching rows from both tables.

SELECT Customers.CustomerID, Customers.Name, Orders.OrderID, Orders.Product
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Customer IDNameOrder IDProduct
2Bob102Phone

LEFT JOIN returns all rows from the left table and matching rows from the right (NULL if no match)

SELECT Customers.CustomerID, Customers.Name, Orders.OrderID, Orders.Product
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Customer IDNameOrder IDProduct
1AliceNULLNULL
2Bob102Phone
3CharlieNULLNULL

RIGHT JOIN returns all rows from the right table and matching rows from the left (NULL if no match)

SELECT Customers.CustomerID, Customers.Name, Orders.OrderID, Orders.Product
FROM Customers
RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Customer IDNameOrder IDProduct
4NULL101Laptop
2Bob102Phone
5NULL103Laptop

Leave a Comment

Your email address will not be published. Required fields are marked *