SQL INNER JOIN - The Coding Shala

Home >> Learn SQL >> SQL INNER JOIN

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

INNER JOIN SYNTAX

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;  

SQL INNER JOIN EXAMPLE

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  
       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  

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  
        from  
        ((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.

Comments

Popular Posts from this Blog

Java Program to Convert Binary to Decimal - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

LeetCode - Shuffle the Array Solution - The Coding Shala

Introduction to Kotlin Programming Language for Backend Development - The Coding Shala