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

Popular posts from this blog

50 Essential SQL Questions to Land Your Dream Job

How to find all the customers who placed orders on three consecutive days | SQL Scenario questions

ACCENTURE SQL INTERVIEW QUESTION | Change the ProductIDs