Saturday, July 25, 2020

JOINS IN SQL SERVER

JOINS: The joins are used to get or retrieve records from more than one tables.It combines rows from two or more tables based on a common column between them. The four types of joins are used. These are:
a) LEFT JOIN
b) INNER JOIN
c) RIGHT JOIN
d) FULL JOIN

a) LEFT JOIN: The LEFT JOIN is also known as LEFT OUTER JOIN. When we use left join then It fetches all records from the left table and the matched records from the right table. And if there is no matching row in the right side table then the result shows NULL. The syntax of Left join is as:

     SELECT 
          COLUMN1(ID), COLUMN2 
     FROM 
          TABLE 1 
     LEFT JOIN 
          TABLE2 
     ON 
          TABLE1.COLUMN1(ID) = TABLE2.COLUMN1(ID)

The TABLE1 is a left side table and TABLE2 is a right side table.

b) INNER JOIN: The INNER JOIN is also known as EQUI JOIN. When we use inner join then it fetches all records that have matched records in two or more tables. The syntax of inner join is as:

      SELECT 
          COLUMN1(ID), COLUMN2 
      FROM 
          TABLE 1 
      JOIN or INNER JOIN
          TABLE2 
      ON 
          TABLE1.COLUMN1(ID) = TABLE2.COLUMN1(ID)

c) RIGHT JOIN: The RIGHT JOIN is also known as RIGHT OUTER JOIN. When we use right join then It fetches all records from the right table and the matched records from the left table. And if there is no matching row in the left side table then the result shows NULL.The right join or right outer join is similar to left join. The syntax of right join is as:

       SELECT 
            COLUMN1(ID), COLUMN2 
       FROM 
           TABLE 1 
       RIGHT JOIN 
           TABLE2 
      ON 
           TABLE1.COLUMN1(ID) = TABLE2.COLUMN1(ID)

The TABLE1 is a left side table and TABLE2 is a right side table.

d) FULL JOIN: The FULL JOIN is also known as FULL OUTER JOIN. When we use full join then It fetches all records from the left table and right table.And if there is no matching row in the right side table then the result shows NULL The syntax of full join is as:

      SELECT 
          COLUMN1(ID), COLUMN2 
      FROM 
          TABLE 1 
      FULL JOIN or FULL OUTER JOIN
          TABLE2 
      ON 
          TABLE1.COLUMN1(ID) = TABLE2.COLUMN1(ID)

The TABLE1 is a left side table and TABLE2 is a right side table.

            - - - - - - Cheers, Happy to Help! - - - - - - 

No comments:

Post a Comment

DIFFERENCE BETWEEN CHAR AND VARCHAR IN SQL SERVER

CHAR:  The  CHAR datatype is a fixed length data type in sql server. It is used to store fixed length type of string data or character strin...