Sample questions
Question 1Easy
How many rows does this query return?
batch_a
name
Anil
Bela
Chirag
Bela
batch_b
name
Bela
Dina
Chirag
NULL
SELECT name FROM batch_a
UNION
SELECT name FROM batch_b;
- 4
- 8
- 6
- 5
Answer: D. 5
Explanation
UNION removes duplicates: Anil, Bela, Chirag, Dina and NULL. Set operations treat NULLs as equal to each other.
Step-by-step solution
- Batch_a gives Anil, Bela, Chirag, Bela; batch_b gives Bela, Dina, Chirag, NULL.
- UNION removes duplicates: Anil, Bela, Chirag, Dina and one NULL.
- That is 5 distinct rows (option D).
Why the other options are wrong
- A. 4 is the count of batch_a rows alone and ignores batch_b.
- B. 8 is UNION ALL's count, which keeps duplicates.
- C. 6 would keep one extra duplicate, but UNION removes all repeats.
Tip: UNION removes duplicates, including repeated NULLs; UNION ALL keeps every row.
Question 2Easy
How many rows does this query return?
batch_a
name
Anil
Bela
Chirag
Bela
batch_b
name
Bela
Dina
Chirag
NULL
SELECT name FROM batch_a
UNION ALL
SELECT name FROM batch_b;
- 7
- 8
- 5
- 6
Answer: B. 8
Explanation
UNION ALL keeps every row from both inputs, duplicates included: 4 + 4 = 8.
Step-by-step solution
- UNION ALL keeps every row from both inputs.
- Batch_a has 4 rows and batch_b has 4 rows.
- 4 + 4 = 8 rows (option B).
Why the other options are wrong
- A. 7 drops one row, but no row is removed by UNION ALL.
- C. 5 is the UNION count after removing duplicates, not UNION ALL.
- D. 6 is not the sum of the two row counts.
Tip: Count rows with UNION ALL as the sum of both inputs; UNION gives the distinct count.
Question 3Easy
How many rows does this query return?
enrollments
student | course
Ira | DBMS
Ira | OS
Jay | DBMS
Kavi | OS
Kavi | DBMS
Kavi | CN
Lia | NULL
SELECT student FROM enrollments WHERE course = 'DBMS'
UNION
SELECT student FROM enrollments WHERE course = 'OS';
- 4
- 3
- 2
- 5
Answer: B. 3
Explanation
Ira, Jay, Kavi from DBMS and Ira, Kavi from OS; UNION removes the duplicates.
Step-by-step solution
- DBMS: Ira, Jay, Kavi.
- OS: Ira, Kavi.
- UNION removes duplicates: Ira, Jay, Kavi = 3.
Why the other options are wrong
- A. There are only three distinct students.
- C. Jay, Ira and Kavi all appear.
- D. Lia has NULL course and is not matched.
Tip: UNION = distinct.
Question 4Easy
What does this query return?
batch_a
name
Anil
Bela
Chirag
Bela
batch_b
name
Bela
Dina
Chirag
NULL
SELECT name FROM batch_a WHERE name LIKE 'C%'
UNION
SELECT name FROM batch_b WHERE name LIKE 'D%'
ORDER BY name;
- Dina
- Chirag, Chirag, Dina
- Chirag, Dina
- Chirag
Answer: C. Chirag, Dina
Explanation
Each branch filters its own table; UNION removes nothing here because Chirag and Dina are distinct.
Step-by-step solution
- First part: batch_a names starting with C is Chirag.
- Second part: batch_b names starting with D is Dina.
- UNION keeps both; sorted, the answer is Chirag, Dina (option C).
Why the other options are wrong
- A. Dina is right, but Chirag from batch_a is also returned.
- B. Chirag appears once after UNION removes duplicates.
- D. Dina from batch_b is also returned.
Tip: Filter each branch separately; ORDER BY after the last branch sorts the combined result.
Question 5Medium
What happens when this query runs?
SELECT 1, 2
UNION
SELECT 3;
- Error: the SELECTs have a different number of result columns
- Returns rows (1, 2) and (3, NULL)
- Returns rows 1, 2, 3
- Returns only (1, 2)
Answer: A. Error: the SELECTs have a different number of result columns
Explanation
Every branch of a set operation must produce the same number of columns.
Step-by-step solution
- The first SELECT returns 2 columns (1, 2).
- The second SELECT returns 1 column (3).
- UNION needs the same column count, so the query raises an error (option A).
Why the other options are wrong
- B. The branches have different column counts, so no row pairing is possible.
- C. The result cannot be a single column because the first branch has two columns.
- D. A set operation does not drop a branch; it raises an error.
Tip: Set operations need equal column counts in every branch; pad with NULL if you need one more column.
Question 6Medium
What does this query return?
matches
mid | home | away | hg | ag
1 | Lions | Tigers | 2 | 1
2 | Bears | Lions | 0 | 0
3 | Tigers | Bears | 3 | 2
4 | Lions | Bears | 1 | 3
5 | Tigers | Lions | NULL | NULL
SELECT home FROM matches WHERE hg > ag
INTERSECT
SELECT away FROM matches WHERE ag > hg;
- Bears
- Lions
- Tigers
- No rows
Answer: D. No rows
Explanation
- Home winners are Lions (match 1) and Tigers (match 3);
- the only away winner is Bears (match 4).
No team is in both lists.
Step-by-step solution
- Home winners (hg > ag): Lions in match 1, Tigers in match 3.
- Away winners (ag > hg): Bears in match 4.
- No team is in both lists, so INTERSECT returns no rows (option D).
Why the other options are wrong
- A. Bears won only as an away team, so it is not in the home-winner list.
- B. Lions won only as a home team, so it is not in the away-winner list.
- C. Tigers won only as a home team, so it is not in the away-winner list.
Tip: INTERSECT needs the same value in both results; a home win and an away win are different roles.
Question 7Medium
What does this query return?
batch_a
name
Anil
Bela
Chirag
Bela
batch_b
name
Bela
Dina
Chirag
NULL
SELECT name FROM batch_a
INTERSECT
SELECT name FROM batch_b
ORDER BY name;
- Bela, Chirag
- Bela
- Bela, Bela, Chirag
- Anil, Dina
Answer: A. Bela, Chirag
Explanation
INTERSECT returns distinct rows present in both results: Bela and Chirag.
Step-by-step solution
- Distinct batch_a values: Anil, Bela, Chirag.
- Distinct batch_b values: Bela, Dina, Chirag, NULL.
- Rows in both are Bela and Chirag, sorted: Bela, Chirag (option A).
Why the other options are wrong
- B. Chirag is also in both lists, so it must be returned.
- C. INTERSECT returns distinct rows, so Bela appears once.
- D. Anil and Dina are each in only one list, so they are not in the intersection.
Tip: INTERSECT keeps rows present in both results, after removing duplicates.
Question 8Medium
What does this query return?
enrollments
student | course
Ira | DBMS
Ira | OS
Jay | DBMS
Kavi | OS
Kavi | DBMS
Kavi | CN
Lia | NULL
SELECT student FROM enrollments WHERE course = 'DBMS'
EXCEPT
SELECT student FROM enrollments WHERE course = 'OS';
- Jay
- Jay, Lia
- Ira, Jay
- Kavi
Answer: A. Jay
Explanation
DBMS students are Ira, Jay, Kavi; removing OS students (Ira, Kavi) leaves Jay.
Step-by-step solution
- DBMS students: Ira, Jay, Kavi.
- OS students: Ira, Kavi. EXCEPT removes students in the second query.
- Only Jay is left (option A).
Why the other options are wrong
- B. Lia's course is NULL, not DBMS, so she is not in the first query.
- C. Ira takes OS, so EXCEPT removes him.
- D. Kavi takes OS, so EXCEPT removes him.
Tip: EXCEPT keeps rows from the first query that are not in the second.
Question 9Hard
Which query returns names that appear in exactly one of batch_a and batch_b?
SELECT name FROM batch_a UNION SELECT name FROM batch_bSELECT name FROM batch_a EXCEPT SELECT name FROM batch_bSELECT name FROM batch_a INTERSECT SELECT name FROM batch_b(SELECT name FROM batch_a EXCEPT SELECT name FROM batch_b) UNION (SELECT name FROM batch_b EXCEPT SELECT name FROM batch_a)
Answer: D. (SELECT name FROM batch_a EXCEPT SELECT name FROM batch_b) UNION (SELECT name FROM batch_b EXCEPT SELECT name FROM batch_a)
Explanation
The symmetric difference is (A − B) ∪ (B − A). Some engines need the two EXCEPTs wrapped in subqueries instead of bare parentheses.
Step-by-step solution
- A − B holds names only in batch_a; B − A holds names only in batch_b.
- The union of those two sets holds names in exactly one list.
- That is option D.
Why the other options are wrong
- A. UNION returns names in either list, including names in both.
- B. EXCEPT gives only names in batch_a, so names only in batch_b are missing.
- C. INTERSECT returns names in both lists, which is the opposite.
Tip: Symmetric difference = (A EXCEPT B) UNION (B EXCEPT A).
Question 10Hard
In a query A UNION B INTERSECT C (standard SQL), how is it evaluated?
- As (A INTERSECT C) UNION B
- As A UNION (B INTERSECT C), because INTERSECT has higher precedence
- Strictly left to right as (A UNION B) INTERSECT C
- It is a syntax error without parentheses
Answer: B. As A UNION (B INTERSECT C), because INTERSECT has higher precedence
Explanation
The SQL standard (followed by PostgreSQL and SQL Server) gives INTERSECT higher precedence. SQLite is an exception and evaluates set operators left to right, so use parentheses or subqueries to be safe.
Step-by-step solution
- In standard SQL INTERSECT binds tighter than UNION.
- So A UNION B INTERSECT C is A UNION (B INTERSECT C).
- That is option B; SQLite evaluates left to right, so parentheses are safer.
Why the other options are wrong
- A. This applies UNION last, which is not the standard precedence.
- C. Left-to-right evaluation is how SQLite works, not the standard rule.
- D. The query is valid SQL without parentheses.
Tip: Use parentheses when mixing UNION and INTERSECT or EXCEPT, so the order is clear in any database.
Practise all 23 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →