Friday, January 1, 2021

JOIN IN MYSQL

 Join are used to retrives data from two and more tables .table does join with command fileds .

tbl_emp                                                   b).tbl_Dept

 ID   Name  Gender Salary  Dept_id.          ID   Dept     Location     

 1    Tom        M         4000   1                 1    IT         London

 2    Prema    F           5000   3                 2    A/C      INDIA

 3    sam        M         3000    4                 3    Sales   CHINA

 4   James       F          500     2

Inner Join :

Return only the matching rows between both tables.

example:

Select  a.ID,a.Name,a.M,a.Salary,b.Dept

from  tbl_emp  a inner join tbl_Dept  b

on a.ID=b.ID

Left Outer Join :

It is return only matching row but it is retrive all data from left table  .

Select  a.ID,a.Name,a.M,a.Salary,b.Dept

from  tbl_emp  a left outer join tbl_Dept  b

on a.ID=b.ID

Right Outer join :

Select  a.ID,a.Name,a.M,a.Salary,b.Dept

from  tbl_emp  a right outer join tbl_Dept  b

on a.ID=b.ID

Self Join :

Joining table  with itself .

 ID   Name  Gender Salary   Mng_id.      

 1    Tom        M         4000      1            

 2    Prema    F           5000     3           

 3    sam        M         3000     4            

 4   James       F          500      2

Select  e.Name as Emp_name ,e.Name as Manager

from  tbl_emp  e inner  join tbl_emp m

on e.ID=m.Mng_id  


No comments:

Post a Comment

How to Find the Third highest salary in mysql

  set @b:=0; set @b1:=3;   # #select distinct salary,@b:=@b+1 as Row_no from emp select  * from emp where salary=( select salary from ( sele...