How to Join table in SQL Server

The SQL joins used to combine records from two or more tables in a database. A join is a means for combining fields from two tables by using values common to each. There are different types of joins available in SQL like as self-join, inner-join, left-join, and right-join.

Self-join: is used to join a table to itself as if the table were two tables, temporarily renaming at least one table in the SQL statement.

Inner-join: INNER JOIN return all rows from multiple tables where the join condition is match.

Left-join: The LEFT JOIN keyword returns all the rows from the left table (tbl_T), even if there are no matches in the right table (tbl_T1).

Right-join: returns all rows from the right table, even if there are no matches in the left table.

  

Consider the following two tables.

Table : tbl_T 
 
 
 Table : tbl_T1
 
 
 Example of Self Join:

 Query : 
  1. SELECT a.user_T_id, a.user_T_name, b.Ph_no  
  2. FROM tbl_T a, tbl_T1 b where a.user_T_id=b.id  
 Result:
 
 
 
Example of Left-Join :

Query: 
  1. SELECT a.user_T_name,a.user_T_pass,a.user_T_add,   
  2. b.Ph_no,b.create_date  
  3. FROM tbl_T a  
  4. LEFT JOIN tbl_T1 b  
  5. ON a.user_T_id = b.id;  
 Result:

 
Example of Inner-join:

Query: 
  1. SELECT a.user_T_name,a.user_T_pass,a.user_T_add,   
  2. b.Ph_no,b.create_date  
  3. FROM tbl_T a  
  4. INNER JOIN tbl_T1 b  
  5. ON a.user_T_id = b.id;  
 Result:
 
 
Example of Right-join:

Query: 
  1. SELECT a.user_T_name,a.user_T_pass,a.user_T_add,   
  2. b.Ph_no,b.create_date  
  3. FROM tbl_T a  
  4. RIGHT JOIN tbl_T1 b  
  5. ON a.user_T_id = b.id;  
 Result:

 
  
Thanks
Ebook Download
View all
SQL Jobs
Read by 0 people
Download Now!
Learn
View all