sql - join one table if condition met otherwise from another table -
i want know sql query following condition i have 3 tables, table 1 want data, second table1mapping , third master table. i want data table1mapping if related data exists otherwise master table , table 1 . table 1 common. for example: table 1: id name 1 abc table1mapping: table1id country code 1 958 2 ind 89 master country code uk 87 56 ind 45 user can select multiple countries. if selects , ind code should table1mapping , ind should master there no data id 1 , us. code may different in both tables. you didn't provide schema created fantasy data show how can like. description: use 2 left joins in combination coalesce, give first element not null. declare @tab1 table ( id int ) declare @tabmapping table ( id int, anycol varchar(100) ) declare @tabmaster table ( id int, anycol varchar(100) ) insert @tab1 (id) values (1),(2),(3),(4),(5),(...