0

SQL Puzzle – 1

Reading Time: 1 minute

What are the various ways of retrieving the shaded part from the below diagram

SQL Problem

So here are the various of ways of retrieving the result. Every solution mentioned below has its own advantage and disadvantages. Here I have assumed both tables has a column named “id”
Solution 1

SELECT a.id FROM tableA a INNER JOIN tableB b
ON a.id=b.id;

Solution 2

SELECT a.id FROM tableA a, tableB b
WHERE a.id=b.id;

Solution 3

SELECT a.id FROM tableA a LEFT JOIN tableB b
ON a.id=b.id 
WHERE b.id IS NOT NULL;

Solution 4

SELECT a.id FROM tableA a RIGHT JOIN tableB b
ON a.id=b.id 
WHERE a.id IS NOT NULL;

Solution 5

SELECT a.id FROM tableA a 
INTERSECT 
SELECT b.id FROM tableB b;

Solution 6

SELECT a.id FROM tableA a 
WHERE a.id IN 
(SELECT b.id FROM tableB b);

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.