SQL Join

 SQL join clause is used to combine two tables based on a common column between them.

      There are the following types of SQL Joins:

      1. INNER JOIN
      2. LEFT JOIN
      3. RIGHT JOIN
      4. FULL JOIN

Let's understand about each join in detail:
1. INNER JOIN:
         Inner join select those records that have matching values in both tables.

         Syntax:
               SELECT col1, col2,...colN 
               FROM table_one INNER JOIN table_two 
               ON table_one.col_name=table_two.col_name;

        Example: Consider the following Employee and Department table

Employee Table
emp_idemp_fnameemp_lnamecitysalarydept_id
1AnupMittalUK60000101
2JohnDoeBangalore50000102
3NamitaThankurDelhi55000101
4AmanSinghUK65000103
5PratikThaparDelhi62000105

Department Table
dept_iddept_name
101Computer Science
102Mechanical
103Civil
104Electrical

        SELECT * FROM Employee INNER JOIN Department 
        ON Employee.dept_id = Department.dept_id;

Output:

emp_idemp_fnameemp_lnamecitysalarydept_iddept_iddept_name
1AnupMittalUK60000101101Computer Science
2JohnDoeBangalore50000102102Mechanical
3NamitaThankurDelhi55000101101Computer Science
4AmanSinghUK65000103103Civil

        You can also select specific columns from both the tables using their table name while performing inner join as:
         SELECT table_one.col1, table_two.col2 
         FROM table_one INNER JOIN table_two 
         ON table_one.col_name = table_two.col_name;

Note: You can join multiple tables using inner join at the same time.

2. LEFT JOIN/LEFT OUTER JOIN:
         Let say we have two tables S and R then S LEFT JOIN R returns all records from S and matching records from R, if doesn't match then it will store a NULL value.

         Syntax:
              SELECT col1, col2, ..., colN 
              FROM table_one LEFT JOIN table_two 
              ON table_one.col_name= table_two.col_name;

         Example:
               SELECT * FROM Employee LEFT JOIN Department 
               ON Employee.dept_id = Department.dept_id;

emp_idemp_fnameemp_lnamecitysalarydept_iddept_iddept_name
1AnupMittalUK60000101101Computer Science
2JohnDoeBangalore50000102102Mechanical
3NamitaThankurDelhi55000101101Computer Science
4AmanSinghUK65000103103Civil
5PratikThaparDelhi62000105NULLNULL

3. RIGHT JOIN/RIGHT OUTER JOIN:
        Let say we have two tables S and R then S RIGHT JOIN R returns all records from R and matching records from S, if doesn't match then it will store a NULL value.

        Syntax:
               SELECT col1, col2, ..., colN 
               FROM table_one LEFT JOIN table_two 
               ON table_one.col_name= table_two.col_name;

         Example:
                SELECT * FROM Employee RIGHT JOIN Department 
                ON Employee.dept_id = Department.dept_id;

emp_idemp_fnameemp_lnamecitysalarydept_iddept_iddept_name
1AnupMittalUK60000101101Computer Science
2JohnDoeBangalore50000102102Mechanical
3NamitaThankurDelhi55000101101Computer Science
4AmanSinghUK65000103103Civil
NULLNULLNULLNULLNULLNULL104Electrical

4. FULL JOIN/FULL OUTER JOIN:
         Full outer join returns all records that matches in left or right table.
Note: FULL OUTER JOIN and FULL JOIN are same.

        Syntax:
                SELECT col1, col2, ..., colN 
                FROM table_one FULL JOIN table_two 
                ON table_one.col_name= table_two.col_name;

        Note: MySQL server does not support FULL JOIN
        
        Example:
               SELECT * FROM Employee FULL JOIN Department
               ON Employee.dept_id = Department.dept_id;

emp_idemp_fnameemp_lnamecitysalarydept_iddept_iddept_name
1AnupMittalUK60000101101Computer Science
2JohnDoeBangalore50000102102Mechanical
3NamitaThakurDelhi55000101101Computer Science
4AmanSinghUK65000103103Civil
5PratikThaparDelhi62000105NULLNULL
6NULLNULLNULLNULLNULL104Electrical

Continue Learning: SQL LIKE Operator 


Comments

Popular Posts