what is left join in sql

10 months ago 55
Nature

In SQL, the LEFT JOIN clause is used to combine two tables based on a specified condition. The main features of a LEFT JOIN are:

  • It returns all records from the left table and the matching records from the right table
  • If there is no match between the tables, the result is NULL from the right side
  • The left table is mandatory, and the right table is optional

The syntax for a LEFT JOIN is as follows:

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

For example, consider two tables, Customers and Orders. A LEFT JOIN can be used to select all customers and any orders they might have, even if there are no matches in the Orders table:

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;

In this query, the LEFT JOIN returns all records from the Customers table, even if there are no matches in the Orders table