SQL INTERVIEW QUESTION | Join 3 Tables & Filter Records in 2 Tables Only
--query to join three tables and filter the results to show only records that exist in exactly two of the tables.
CREATE TABLE TableA (
ID INT,
ValueA VARCHAR(50)
);
CREATE TABLE TableB (
ID INT,
ValueB VARCHAR(50)
);
CREATE TABLE TableC (
ID INT,
ValueC VARCHAR(50)
);
TRUNCATE TABLE TableA
TRUNCATE TABLE TableB
TRUNCATE TABLE TableC
INSERT INTO TableA (ID, ValueA) VALUES (1, 'CommonValue'), (2, 'CommonValue'), (3, 'UniqueA'),(5,'CommonValue');
INSERT INTO TableB (ID, ValueB) VALUES (2, 'CommonValue'), (3, 'CommonValue'), (4, 'UniqueB'),(5,'CommonValue');
INSERT INTO TableC (ID, ValueC) VALUES (3, 'CommonValue'), (4, 'CommonValue'), (2, 'UniqueC'),(5,'CommonValue');
Comments
Post a Comment