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?
- They must return the same number of columns, with compatible data types in matching positions
- They must read from the same table
- Their columns must have exactly the same names
- 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?
- The UNION ALL has 2 columns and 8 rows; the cross join has 4 columns and 8 rows
- The UNION ALL has 1 column and 15 rows; the cross join has 4 columns and 8 rows
- Both have 1 column and 8 rows
- 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?
- 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: 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
- Table Cricket: student Aman Bina Chetan Bina Table Football: student Bina…
- Table Cricket: student Aman Bina Chetan Bina Table Football: student Bina…
- Table Cricket: student Aman Bina Chetan Bina Table Football: student Bina…
- Table Cricket: student Aman Bina Chetan Bina Table Football: student Bina…
- Which condition must two SELECT statements satisfy before they can be combined…
- Two queries are known to have no rows in common, and neither has duplicates…
- A query is written as SELECT emp_name FROM Staff UNION SELECT trainee_name…
- Table MondaySale: item Tea Samosa Tea Vada Table TuesdaySale: item Samosa Poha…
- Table Employee: id | name 1 | Kavya 2 | Lokesh Table Intern: id | name |…
- Table SectionA: name | marks Gita | 70 Hari | 90 Table SectionB: name | marks…
- Table Store: city | product Pune | Pen Pune | Ink Table Depot: city | product…
- Table Cricket: student Aman Bina Chetan Bina Table Football: student Bina…
- Table Cricket: student Aman Bina Chetan Bina Table Football: student Bina…
- Table SectionA: name | marks Gita | 70 Hari | 90 Table SectionB: name | marks…
- Table Customer(cust_id, name) has 5 rows and table Supplier(supp_id, name) has…
- 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…
- Tables A and B each have a column id that is declared NOT NULL. A.id can…
- Table Cricket: student Aman Bina Chetan Bina The same table is used on both…
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
- SELECT, WHERE & ORDER BY: Filtering, DISTINCT, IN, BETWEEN, LIKE, sorting and LIMIT.
- SQL Joins: Inner, left, right, full, cross and self joins, and row counts.
- Aggregates & GROUP BY: COUNT, SUM, AVG, grouping and HAVING versus WHERE.
- Subqueries & CTEs: Scalar and correlated subqueries, IN, EXISTS and WITH.
- Window Functions: ROW_NUMBER, RANK, DENSE_RANK, PARTITION BY, LAG and running totals.
- NULL Handling: Three-valued logic, IS NULL, COALESCE and NULL in aggregates.
- SQL Functions & CASE: CASE WHEN, conditional aggregation, string and numeric functions.
- Tables, Keys & Constraints: CREATE, ALTER, primary and foreign keys, UNIQUE and CHECK.
- DML & Transactions: INSERT, UPDATE, DELETE, COMMIT, ROLLBACK and ACID.
Other subjects
- Quantitative Aptitude: Arithmetic, percentages, time and work, speed, probability and data interpretation: the numerical section of every fresher test.
- Logical Reasoning: Series, coding-decoding, arrangements, syllogisms and puzzles: the section that separates prepared candidates from the rest.
- Verbal Ability: Vocabulary, grammar, reading comprehension and sentence order: tested in nearly every IT services hiring exam.
- Technical & Programming: CS fundamentals, output prediction and pseudo-code: the technical MCQ round for IT roles.
- Python: Python quizzes by topic, from types and strings to classes and generators: output prediction and interview concepts.
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