UNION, INTERSECT & EXCEPT · Question 19 · Hard
UNION, INTERSECT & EXCEPT question 19
Tables A and B each have a column id that is declared NOT NULL. A.id can contain repeated values. Which query always returns exactly the same rows as SELECT id FROM A EXCEPT SELECT id FROM B?
- SELECT id FROM A WHERE id NOT IN (SELECT id FROM B)
- SELECT DISTINCT A.id FROM A JOIN B ON A.id <> B.id
- SELECT DISTINCT id FROM A WHERE id NOT IN (SELECT id FROM B)
- SELECT DISTINCT A.id FROM A LEFT JOIN B ON A.id = B.id WHERE B.id IS NOT NULL
Answer and explanation
Answer: C. SELECT DISTINCT id FROM A WHERE id NOT IN (SELECT id FROM B)
Explanation: EXCEPT returns the distinct ids of A that do not occur in B. NOT IN finds the ids missing from B (safe here because id is NOT NULL), and DISTINCT is needed because EXCEPT removes repeats. The <> join keeps almost every id, and the last query returns the ids that are in both tables.
Previous: Table Cricket: student Aman Bina Chetan Bina Table Football: student Bina… · Next: Table Cricket: student Aman Bina Chetan Bina The same table is used on both…
Practise union, intersect & except
Try a fresh set of union, intersect & except questions with an explanation after every answer, free and without signing up: UNION, INTERSECT & EXCEPT practice. Or sit a full placement mock test.
More union, intersect & except questions
- Table Cricket: student Aman Bina Chetan Bina Table Football: student Bina…
- Table MayOrders: order_id | coupon 1 | SAVE10 2 | NULL 3 | NULL Table…
- Table Cricket: student Aman Bina Chetan Bina Table Football: student Bina…
- Table Cricket: student Aman Bina Chetan Bina The same table is used on both…
More sql practice
Tables, Keys & Constraints · DML & Transactions · SELECT, WHERE & ORDER BY · SQL Joins · Aggregates & GROUP BY · Subqueries & CTEs · Window Functions · NULL Handling
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge