Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi I have two tables tab1 and tab2.
Tab1
id Name
1 abc
2 xyz
3 mno

tab2
id salary
1 100
2 200
3 300

I want output like as follows without using JOIN

id Name Salary
1 abc 100
2 xyz 200
3 mno 300
Posted
Comments
PIEBALDconsult 20-Jun-15 15:39pm    
But JOIN is the right tool for the job. Why not use it? Is this an interview or test question?
Frankie-C 20-Jun-15 17:06pm    
It's an homework...
dinesh42 21-Jun-15 4:41am    
Its a interview question asked to me :P

Why not JOIN?
You can still get the required result, though.
Try this-

SQL
SELECT Id,Name, ( SELECT Salary FROM Tab2 WHERE Tab1.Id=Tab2.Id) AS Salary
FROM Tab1


Note: Haven't runned this quey in SSMS but something like this should work.
 
Share this answer
 
v3
Comments
dinesh42 21-Jun-15 4:41am    
Thank you Giri. It worked. I have used the correlated sub query in Where clause, but not in select clause.

Learnt one new point :-)
select id,name,salary from tab1,tab2 where tab1.id=tab2.id
but this is a kind of join
 
Share this answer
 

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