Header Ads Widget

SQL JOIN

SQL JOIN

As the name shows, JOIN means to combine something. In case of SQL, JOIN means "to combine two or more tables".

In SQL, JOIN clause is used to combine the records from two or more tables in a database.

Types of SQL JOIN

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

Sample Table

EMPLOYEE

EMP_IDEMP_NAMECITYSALARYAGE
1AngelinaChicago20000030
2RobertAustin30000026
3ChristianDenver10000042
4KristenWashington50000029
5RussellLos angels20000036
6MarryCanada60000048

PROJECT

PROJECT_NOEMP_IDDEPARTMENT
1011Testing
1022Development
1033Designing
1044Development

1. INNER JOIN

In SQL, INNER JOIN selects records that have matching values in both tables as long as the condition is satisfied. It returns the combination of all rows from both the tables where the condition satisfies.

Syntax

  1. SELECT table1.column1, table1.column2, table2.column1,....  
  2. FROM table1   
  3. INNER JOIN table2  
  4. ON table1.matching_column = table2.matching_column;  

Query

  1. SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT   
  2. FROM EMPLOYEE  
  3. INNER JOIN PROJECT  
  4. ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;  

Output

EMP_NAMEDEPARTMENT
AngelinaTesting
RobertDevelopment
ChristianDesigning
KristenDevelopment

2. LEFT JOIN

The SQL left join returns all the values from left table and the matching values from the right table. If there is no matching join value, it will return NULL.

Syntax

  1. SELECT table1.column1, table1.column2, table2.column1,....  
  2. FROM table1   
  3. LEFT JOIN table2  
  4. ON table1.matching_column = table2.matching_column;  

Query

  1. SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT   
  2. FROM EMPLOYEE  
  3. LEFT JOIN PROJECT  
  4. ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;  

Output

EMP_NAMEDEPARTMENT
AngelinaTesting
RobertDevelopment
ChristianDesigning
KristenDevelopment
RussellNULL
MarryNULL

3. RIGHT JOIN

In SQL, RIGHT JOIN returns all the values from the values from the rows of right table and the matched values from the left table. If there is no matching in both tables, it will return NULL.

Syntax

  1. SELECT table1.column1, table1.column2, table2.column1,....  
  2. FROM table1   
  3. RIGHT JOIN table2  
  4. ON table1.matching_column = table2.matching_column;  

Query

  1. SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT   
  2. FROM EMPLOYEE  
  3. RIGHT JOIN PROJECT  
  4. ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;  

Output

EMP_NAMEDEPARTMENT
AngelinaTesting
RobertDevelopment
ChristianDesigning
KristenDevelopment

4. FULL JOIN

In SQL, FULL JOIN is the result of a combination of both left and right outer join. Join tables have all the records from both tables. It puts NULL on the place of matches not found.

Syntax

  1. SELECT table1.column1, table1.column2, table2.column1,....  
  2. FROM table1   
  3. FULL JOIN table2  
  4. ON table1.matching_column = table2.matching_column;  

Query

  1. SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT   
  2. FROM EMPLOYEE  
  3. FULL JOIN PROJECT   
  4. ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;  

Output

EMP_NAMEDEPARTMENT
AngelinaTesting
RobertDevelopment
ChristianDesigning
KristenDevelopment
RussellNULL
MarryNULL

Post a Comment

0 Comments