• Home
  • SQL LEFT JOIN Keyword

    In SQL, a LEFT JOIN (or LEFT OUTER JOIN) is used to combine rows from two tables based on a related column, while including all rows from the left table and only matching rows from the right table. The syntax for a LEFT JOIN is as follows:

    sql
    SELECT columns
    FROM table1
    LEFT JOIN table2 ON table1.column = table2.column;

    In this syntax:

    - `table1` is the left table from which you want to retrieve all rows.
    - `table2` is the right table that you want to join with `table1`.
    - `column` is the common column between the two tables that you want to use for the join condition.
    - `columns` represent the columns you want to retrieve from both tables.


    Here's an example that demonstrates a LEFT JOIN:

    Let's say we have two tables: `orders` and `customers`. The `orders` table contains information about orders, and the `customers` table contains information about customers. We want to retrieve all orders along with the corresponding customer information, including orders without any matching customer records.

    sql
    SELECT orders.order_id, orders.order_date, customers.customer_name
    FROM orders
    LEFT JOIN customers ON orders.customer_id = customers.customer_id;

    In this query, we are performing a LEFT JOIN between the `orders` and `customers` tables based on the `customer_id` column. This will include all rows from the `orders` table, even if there is no matching customer record in the `customers` table.

    The result of the LEFT JOIN will contain all rows from the left table (`orders`) and matching rows from the right table (`customers`). If there is no matching row in the right table, the columns from the right table will contain NULL values.

    LEFT JOINs are commonly used when you want to retrieve all records from one table along with matching records from another table, regardless of whether there is a match or not.



    About the Author



    Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

    We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





     PreviousNext