MySQL LEFT JOIN

Syntax :

SELECT columns FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name;

LEFT JOIN keyword returns all records from the left table (table1) and the matching records from the right table (table2). If the records did not match from the right side, then NULL is returned as value for those records. Insert data into certain columns you will need to specify only these columns as part of the statement.

Below are the examples to understand the LEFT JOIN clause.

Table 1 : customers

MYSQL Customers Table

 Table 2 : orders

MYSQL Orders Table

MYSQL LEFT JOIN using clause

Joining the two tables and get customers list with order details

SELECT customers.customer_name, customers.cust_mobileno, customers.cust_location, orders.order_id,orders.amount, orders.order_date FROM customers LEFT JOIN orders ON customers.customer_id=orders.customer_id

After execution of above query, the following records will display as output.

MYSQL LEFT JOIN using clause

MYSQL LEFT JOIN using WHERE clause

Joining the two tables and get customers list with order details based on values in the column amount

SELECT customers.customer_name, customers.cust_mobileno, customers.cust_location, orders.order_id,orders.amount, orders.order_date FROM customers LEFT JOIN orders ON customers.customer_id=orders.customer_id WHERE orders.amount>5000;

After execution of above query, the following records will display as output.

MYSQL LEFT JOIN using WHERE clause