MySQL Wildcards

MySQL Wildcards

MySQL Aliases

MySQL Aliases

MySQL Data Types

MySQL Data Types

MySQL Interview Questions

MySQL Interview Questions and Answers


MySQL LEFT JOIN

In MySQL, LEFT JOIN is used to fetch all records from LEFT side table (tablename1) and only matching records from right side table (tablename2). It appears immediately after the FROM clause.

MySQL LEFT JOIN Syntax

To fetch all records from LEFT side table (tablename1) and only matching records from right side table (tablename2), use the following syntax:

Syntax

SELECT col1, col2,....
FROM tablename1
LEFT JOIN tablename2 ON tablename1.col1 = tablename2.col1;

Parameters:

tablename1 & tablename2: The table name from which you want to fetch records.

col1: The column or field name that will be return.

MySQL LEFT JOIN example

The following MySQL, fetching all "accountName" fields value from "tblbankdemataccountcharges" table and only matching record from "banks" field from "tblbankheadquartersandaddress" table using LEFT JOIN, both have matching column values in tables :

Example

SELECT A.accountName, B.banks 
FROM tblbankdemataccountcharges A 
LEFT JOIN tblbankheadquartersandaddress B
ON A.bankId = B.pk LIMIT 6;

Note:

In the above example, we are fetching all "accountName" fields value from "tblbankdemataccountcharges" table only matching record from "banks" field from "tblbankheadquartersandaddress" table using LEFT JOIN, both have matching column values in tables.

You can use MySQL Command Line Client to fetch all records from left table and only matching record from right table where the join condition is satisfied. It will look like this: