SQL JOINS
JOIN clause is used to combine rows from two or more tables based on a common field.
- INNER JOIN: returns all rows from both tables as long as there is a match between the columns in both tables. All the rows that did not match in the criteria in both the tables will not be returned.
SELECT * FROM Orders AS O INNER JOIN Customers AS C ON O.CustomerID = C.CustomerID
- LEFT JOIN: returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.
SELECT * FROM Customers AS C LEFT JOIN Orders AS O ON C.CustomerID = O.CustomerID
- RIGHT JOIN: returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.
SELECT * FROM Orders AS O RIGHT JOIN Customers AS C ON O.CustomerID = C.CustomerID - FULL JOIN: returns all rows from the left table (table1) and from the right table (table2).
SELECT * FROM Customers FULL OUTER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
Comments
Post a Comment