SQL Tutorial
In SQL, a JOIN
clause is used to combine rows from two or more tables, based on a related column between them. The JOIN operation allows you to query data from multiple tables as if the data were in one table.
There are four main types of SQL JOIN
operations:
INNER JOIN: The INNER JOIN
keyword selects records that have matching values in both tables.
SELECT Orders.OrderID, Customers.CustomerName FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
LEFT (OUTER) JOIN: The LEFT JOIN
keyword returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side if there is no match.
SELECT Customers.CustomerName, Orders.OrderID FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
RIGHT (OUTER) JOIN: The RIGHT JOIN
keyword returns all records from the right table (table2), and the matched records from the left table (table1). The result is NULL from the left side when there is no match.
SELECT Orders.OrderID, Customers.CustomerName FROM Orders RIGHT JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
FULL (OUTER) JOIN: The FULL JOIN
keyword returns all records when there is a match in either the left (table1) or the right (table2) table records.
SELECT Customers.CustomerName, Orders.OrderID FROM Customers FULL JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Remember, the LEFT JOIN
and RIGHT JOIN
are the same operation; the difference lies in the order of the tables in the JOIN operation.
Note: The syntax may differ slightly depending on the specific SQL dialect (MySQL, PostgreSQL, SQL Server, etc.) you're using, so always check the specific documentation if you encounter issues. Also, not all SQL dialects support all types of JOIN operations. For example, MySQL doesn't support the FULL JOIN
operation. In such cases, you'd have to emulate it using a combination of LEFT
and RIGHT JOIN
operations.
INNER JOIN in SQL:
SELECT Orders.OrderID, Customers.CustomerName FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
LEFT JOIN vs RIGHT JOIN in SQL:
Description: LEFT JOIN retrieves all rows from the left table and matching rows from the right table. RIGHT JOIN does the opposite.
Code Example (LEFT JOIN):
SELECT Orders.OrderID, Customers.CustomerName FROM Orders LEFT JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
Code Example (RIGHT JOIN):
SELECT Orders.OrderID, Customers.CustomerName FROM Orders RIGHT JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
Using WHERE Clause with SQL JOIN:
SELECT Orders.OrderID, Customers.CustomerName FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID WHERE Orders.OrderDate > '2022-01-01';
SQL JOIN with Multiple Conditions:
SELECT Orders.OrderID, Customers.CustomerName FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID AND Orders.Shipped = 1;
Combining Multiple Tables with SQL JOIN:
SELECT Orders.OrderID, Customers.CustomerName, Products.ProductName FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID;
Handling NULL Values in SQL JOIN:
SELECT Orders.OrderID, COALESCE(Customers.CustomerName, 'Unknown') AS CustomerName FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;