SQL Join Types
INNER JOIN
An INNER JOIN returns records that have matching values in both tables.
Tables:
Customers | |
---|---|
ID | Name |
1 | John |
Orders | |
---|---|
OrderID | CustomerID |
1 | 1 |
SQL Query:
SELECT Customers.Name, Orders.OrderID FROM Customers INNER JOIN Orders ON Customers.ID = Orders.CustomerID;
LEFT JOIN
A LEFT JOIN returns all records from the left table, and the matched records from the right table.
SELECT Customers.Name, Orders.OrderID FROM Customers LEFT JOIN Orders ON Customers.ID = Orders.CustomerID;
RIGHT JOIN
A RIGHT JOIN returns all records from the right table, and the matched records from the left table.
SELECT Customers.Name, Orders.OrderID FROM Customers RIGHT JOIN Orders ON Customers.ID = Orders.CustomerID;
FULL JOIN
A FULL JOIN returns all records when there is a match in either the left or right table records.
SELECT Customers.Name, Orders.OrderID FROM Customers FULL JOIN Orders ON Customers.ID = Orders.CustomerID;