A hump that most people starting out in SQL have to get over is how joins work - that is, thinking relational.
idx1 int,
col1 text,
join_col text
);
idx2 int,
col2 text,
join_col text
);
insert into test_legit_matches_1 values (2, 'val2', 'join me');
insert into test_legit_matches_1 values (3, 'val3', 'nice join');
insert into test_legit_matches_2 values (1, 'val4', 'join me');
insert into test_legit_matches_2 values (2, 'val5', 'join me');
insert into test_legit_matches_2 values (3, 'val6', 'nice join');
select
*
from test_legit_matches_1 as t1
on t1.join_col = t2.join_col
;
with _joined as (
select
*
, row_number() over (partition by idx1 order by idx2) as idx1_rn
, row_number() over (partition by idx2 order by idx1) as idx2_rn
from test_legit_matches_1 as t1
on t1.join_col = t2.join_col
from _joined
where idx1_rn = idx2_rn
Result 1:
Result 2: