Kapil Kapil - 4 months ago 9
SQL Question

SQL - Check record exists in 2nd table and display the result in main table as separate column

SQL - Check record exists in 2nd table and display the result in main table as separate column

I have 2 tables TAB1 and TAB2 as below :

TAB1

Sid Sname Svalue
===================
1 Joe 22
2 Derk 30
3 Derk1 40

TAB2

id name SID
===================
1 Joe 2
2 Derk 3


Result Should display as :

Sid Sname Svalue Status
============================
1 Joe 22 false
2 Derk 30 true
3 Derk1 40 true


can any one build the query

Answer
SELECT t1.Sid,  t1.Sname,  t1.Svalue, CASE WHEN t2.SID IS NULL THEN 'False' ELSE 'True' END AS Status
FROM Tab1 t1
LEFT OUTER JOIN Tab2  t2
   ON t1.SID = t2.SID