SQL LEFT JOIN - The Coding Shala

Home >> Learn SQL >> SQL LEFT JOIN

SQL LEFT JOIN

The SQL LEFT JOIN is a type of OUTER JOIN. The SQL LEFT JOIN or SQL LEFT OUTER JOIN returns all the records from the left table and matched records from the right table. If there is no match in the right table then it returns NULL values from the right table.
SQL LEFT JOIN - The Coding Shala

SQL LEFT JOIN SYNTAX

The basic syntax of the LEFT JOIN is as follows -  

 SELECT table1.column1, table2.column2, ..  
 FROM table1  
 LEFT JOIN table2 ON table1.column_name = table2.column_name;  

SQL LEFT JOIN Example

The following two tables are used for the example - 
Table 1. 'emp' table 

emp_id     emp_name     city     dept_no     salary  
 1          Akshay        Pune      101       50000  
 3          Nikhil        Pune      101       51000  
 5          Mohit         Delhi     103       40000  
 6          Shubham       Surat     105       42000  
 7          Akash         Mumbai    106       45000  

Table 2. 'dept' table 

dept_no     dept_name      total_emp  
 101          Product Dev       50  
 102          Consulting       100  
 103          Product Consult   20  
 104          Marketing        150  
 105          Sales            250 

The following SQL statement is an example of SQL LEFT JOIN - 

SQL >> select emp.emp_id, emp.dept_no, emp.emp_name,  
       dept.dept_name  
       from emp  
       left join dept on emp.dept_no = dept.dept_no;  
 Output >>  
 emp_id     dept_no     emp_name     dept_name  
 1          101          Akshay          Product Dev  
 3          101          Nikhil          Product Dev  
 5          103          Mohit          Product Consult  
 6          105          Shubham       Sales  
 7          106          Akash          null  



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Add two numbers in Scala - The Coding Shala

New Year Chaos Solution - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala