Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have first table as follows


Branchid BranchName

1 A
2 B
3 C
4 D
5 E


I have Second table as follows


Empid Empname Branchid

1 Ram 1
2 Suresh 2
3 Vinay 1
4 Rahul -
5 Sam 3


i want to get output of combine the above two tables branch id. for which one branch id is there and branch id is not there

for that how to write the query

What I have tried:

 I have first table as follows


  Branchid         BranchName

    1                A
    2                B
    3                C
    4                D
    5                E


I have Second table as follows


 Empid    Empname     Branchid
 
   1        Ram          1
   2        Suresh       2
   3        Vinay        1
   4        Rahul        -
   5        Sam          3
   

i want to get output of combine the above two tables branch id. for which one branch id is there  and branch id is not there

for that how to write the query
Posted
Updated 12-Mar-17 9:12am
Comments
Michael_Davies 12-Mar-17 12:27pm    
In the second table where there is no Branchid is it Null or do you put the dash in when no branch?

Use the appropriate SQL Joins[^] to achieve your objective, e.g.
select b.branchid, b.branchname, e.empid, e.empname from branchtable b right join emptable e on
b.branchid = e.branchid
will give you
branchid	branchname	empid	empname
1		      A		     1	    Ram
2		      B		     2	    Suresh
1		      A		     3	    Vinay
NULL		  NULL	     4	    Rahul
3		      C		     5	    Sam
 
Share this answer
 
Select information from your second table joined to your first. A left outer join would be appropriate.
See
Visual Representation of SQL Joins[^]

You might also find the ISNULL function useful
 
Share this answer
 
--------------for first----------------
select e.branchid as branchid,b.branchname,e.empid,e.empname from branch b
join empp e on b.branchid=e.branchid


--------for second-----------
select b.branchid as branchid,b.branchname,e.empid,e.empname,e.branchid as brancid from branch b
left join empp e on b.branchid=e.branchid where empid is null
 
Share this answer
 
Comments
CHill60 13-Mar-17 4:40am    
Your second query does not do what you say it does. It actually lists branchs that have no employees - which is not what the OP wanted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900