By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE Table1(Id INT,Que VARCHAR(100), Ans_Id VARCHAR(100));
CREATE TABLE Table2(Id INT,Choice VARCHAR(100), Choice_Id VARCHAR(100));
INSERT INTO Table1 VALUES(1,'Sample Question #1','a'),(2,'Sample Question #2','c'),(3,'Sample Question #3','b');
INSERT INTO Table2 VALUES(1,'(a) sasas','a'),(1,'(b) saewew','b'),(1,'(c) 4234','c'),
(2,'(a) xxxxx','a'),(2,'(b) 5855szew','b'),(2,'(c) sd4234','c'),
(3,'(a) bbbbbbbb','a'),(3,'(b) saew34','b'),(3,'(c) 42d34','c');
SELECT Q.ID, Q.Que Quetion, A.Choice Options, Op.Choice AS Answer
FROM Table1 Q JOIN Table2 A ON Q.Id= A.Id
LEFT JOIN Table2 Op ON Q.ID = Op.Id AND Q.Ans_Id = Op.Choice_Id
SELECT Q.ID, Q.Que Quetion, A.Choice Options, Op.Choice AS Answer
FROM Table1 Q JOIN Table2 A ON Q.Id= A.Id
LEFT JOIN Table2 Op ON Q.ID = Op.Id AND Q.Ans_Id = Op.Choice_Id AND Op.Choice_Id = A.Choice_Id
ID | Quetion | Options | Answer |
---|---|---|---|
1 | Sample Question #1 | (a) sasas | (a) sasas |
1 | Sample Question #1 | (b) saewew | (a) sasas |
1 | Sample Question #1 | (c) 4234 | (a) sasas |
2 | Sample Question #2 | (a) xxxxx | (c) sd4234 |
2 | Sample Question #2 | (b) 5855szew | (c) sd4234 |
2 | Sample Question #2 | (c) sd4234 | (c) sd4234 |
3 | Sample Question #3 | (a) bbbbbbbb | (b) saew34 |
3 | Sample Question #3 | (b) saew34 | (b) saew34 |
3 | Sample Question #3 | (c) 42d34 | (b) saew34 |
ID | Quetion | Options | Answer |
---|---|---|---|
1 | Sample Question #1 | (a) sasas | (a) sasas |
1 | Sample Question #1 | (b) saewew | null |
1 | Sample Question #1 | (c) 4234 | null |
2 | Sample Question #2 | (a) xxxxx | null |
2 | Sample Question #2 | (b) 5855szew | null |
2 | Sample Question #2 | (c) sd4234 | (c) sd4234 |
3 | Sample Question #3 | (a) bbbbbbbb | null |
3 | Sample Question #3 | (b) saew34 | (b) saew34 |
3 | Sample Question #3 | (c) 42d34 | null |