SQL - Inner Join on duplicate keys column
- Published on
- • 2 mins read•––– views
An interview question to do a inner join on table A and B with duplicate keys
table t1c1---100101
table d1c1---010100
-- USING POSTGRES
create schema temp;
set schema 'temp';
CREATE TABLE t1 ( c1 INT not NULL);
CREATE TABLE d1 ( c1 INT not NULL);
insert into t1 values(1),(0),(0),(1),(0),(1);insert into d1 values(0),(1),(0),(1),(0),(0);
select * from t1 as t1inner join d1 as d1on t1.c1 = d1.c1 ;
Output:
Total Records 18 as output
How 18 records. Why 12 0's and 6 1's?
total value 1 in table t1 = 3total value 1 in table d1 = 2total value 1 in INNER JOIN = 3 * 2 = 6
total value 0 in table t1 = 3total value 0 in table d1 = 4total value 0 in INNER JOIN = 3 * 4 = 12
RESULT======c1 c10 00 00 00 00 00 00 00 00 00 00 00 01 11 11 11 11 11 1