SQL INNER JOIN - The Coding Shala

Home >> Learn SQL >> SQL INNER JOIN


SQL INNER JOIN is the most important and frequently used to join. INNER JOIN is also referred to as EQUIJOIN. The SQL INNER JOIN selects records that have matching values in both tables.
SQL INNER JOIN - The Coding Shala


The basic syntax of SQL INNER JOIN is following - 

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


The following two tables are used for the examples.
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 query will join both emp and dept table and return all the records where dept_no is same on both the tables - 

SQL >> select emp.emp_id, emp.dept_no, emp.emp_name, dept.dept_name  
       from emp  
       inner join dept  
       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  

Like this, we join more than two tables also. 

SQL INNER Join Three Tables

 The following query is an example of SQL INNER Join of three tables. In this example, one more table 'detail' is used that contains emp_id and age. 

SQL >> select emp.emp_id, emp.emp_name,  
        detail.age, dept.dept_no, dept.dept_name  
        ((emp inner join detail on emp.emp_id = detail.emp_id)  
        inner join dept on emp.dept_no = dept.dept_no);  
 Output >>  
 emp_id     emp_name     age     dept_no     dept_name  
 1          Akshay         25      101      Product Dev  
 3           Nikhil        23      101      Product Dev  
 5           Mohit         29      103      Product Consult  
 6          Shubham       26      105      Sales  

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.


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