UNION, INTERSECT & EXCEPT questions and answers

Combining results, duplicates and column rules. Practise union, intersect & except questions from the sql section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.

Sample union, intersect & except questions with worked answers

Question 1

Which condition must two SELECT statements satisfy before they can be combined with UNION?

  1. They must return the same number of columns, with compatible data types in matching positions
  2. They must read from the same table
  3. Their columns must have exactly the same names
  4. They must return the same number of rows

Answer: A. They must return the same number of columns, with compatible data types in matching positions

Explanation: Set operators line the two results up column by column, so the column count must match and each pair of columns must have compatible types. Table names, column names and row counts can differ.

Question 2

Table Customer(cust_id, name) has 5 rows and table Supplier(supp_id, name) has 3 rows. How does the shape of SELECT name FROM Customer UNION ALL SELECT name FROM Supplier differ from that of Customer CROSS JOIN Supplier?

  1. The UNION ALL has 2 columns and 8 rows; the cross join has 4 columns and 8 rows
  2. The UNION ALL has 1 column and 15 rows; the cross join has 4 columns and 8 rows
  3. Both have 1 column and 8 rows
  4. The UNION ALL has 1 column and 8 rows; the cross join has 4 columns and 15 rows

Answer: D. The UNION ALL has 1 column and 8 rows; the cross join has 4 columns and 15 rows

Explanation: A set operator stacks results vertically: the column count stays 1 and the rows add up to 5 + 3 = 8. A join places tables side by side: the columns add up to 2 + 2 = 4 and a cross join has 5 x 3 = 15 rows.

Question 3

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?

  1. SELECT id FROM A WHERE id NOT IN (SELECT id FROM B)
  2. SELECT DISTINCT A.id FROM A JOIN B ON A.id <> B.id
  3. SELECT DISTINCT id FROM A WHERE id NOT IN (SELECT id FROM B)
  4. SELECT DISTINCT A.id FROM A LEFT JOIN B ON A.id = B.id WHERE B.id IS NOT NULL

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.

All 20 union, intersect & except questions with answers

UNION, INTERSECT & EXCEPT: common questions

What are union, intersect & except questions?

Combining results, duplicates and column rules. They appear in the sql section of many placement tests.

Where can I practise union, intersect & except questions with answers for free?

Right here on LogicWiz Exams. Every union, intersect & except question has a worked explanation after you answer. Practice is free and needs no sign-up.

How should I practise union, intersect & except?

Start with easy questions, then move to medium and hard. Read the explanation after each answer, and come back to the ones you missed.

Start practising

On this page you can start a fresh set of union, intersect & except questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: SQL practice.

More sql topics

Other subjects

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge · Test from a job description · LogicWiz Open: free team quiz · Free interview prep