SQL Joins Samples

INNER JOIN

This sample demonstrates the use of an INNER JOIN to combine data from two tables based on a common column.

                
                    SELECT
                        Customers.CustomerID,
                        Customers.ContactName,
                        Orders.OrderID,
                        Orders.OrderDate
                    FROM
                        Customers
                    INNER JOIN
                        Orders ON Customers.CustomerID = Orders.CustomerID;
                
            

Output:

This query returns all records where there is a match between the `CustomerID` in the `Customers` table and the `CustomerID` in the `Orders` table.

Learn more about INNER JOIN

LEFT JOIN

This sample demonstrates the use of a LEFT JOIN to return all records from the left table and matching records from the right table.

                
                    SELECT
                        Customers.CustomerID,
                        Customers.ContactName,
                        Orders.OrderID,
                        Orders.OrderDate
                    FROM
                        Customers
                    LEFT JOIN
                        Orders ON Customers.CustomerID = Orders.CustomerID;
                
            

Output:

This query returns all records from the `Customers` table, and for each customer, it will return the corresponding order details if an order exists for that customer. If a customer has no orders, the order details columns will be `NULL`.

Learn more about LEFT JOIN

RIGHT JOIN

This sample demonstrates the use of a RIGHT JOIN to return all records from the right table and matching records from the left table.

                
                    SELECT
                        Customers.CustomerID,
                        Customers.ContactName,
                        Orders.OrderID,
                        Orders.OrderDate
                    FROM
                        Customers
                    RIGHT JOIN
                        Orders ON Customers.CustomerID = Orders.CustomerID;
                
            

Output:

This query returns all records from the `Orders` table, and for each order, it will return the corresponding customer details if a customer exists for that order. If an order has no customer, the customer details columns will be `NULL`.

Learn more about RIGHT JOIN

Related Samples