By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
select @@version;
(No column name) |
---|
Microsoft SQL Server 2019 (RTM-CU1) (KB4527376) - 15.0.4003.23 (X64) Dec 6 2019 14:53:33 Copyright (C) 2019 Microsoft Corporation Express Edition (64-bit) on Windows Server 2019 Standard 10.0 <X64> (Build 17763: ) (Hypervisor) |
create table TableA (id int, Code1 int, Code2 int)
insert into TableA values
(1, 2000, 1),
(2, 3000, 2),
(3, 1000, 3),
(4, 2100, 1),
(5, 3500, 5)
Create table TableB(Code1 int, Code2 int)
insert into TableB Values
(2100, 7),
(2100, 1),
(3000, 2),
(1000, 4)
Select
TableA.Id
from TableA
where exists(Select 1 from TableB where tableA.Code1 = tableB.Code1
and TableA.Code2 = tableB.Code2)
Id |
---|
2 |
4 |