Sample questions
Question 1Easy
What does this query return?
staff
id | name | dept_id | salary | mgr_id
1 | Om | 1 | 90000 | NULL
2 | Pia | 1 | 60000 | 1
3 | Qadir | 2 | 75000 | 1
4 | Riya | 2 | 50000 | 3
5 | Sam | NULL | 40000 | 3
6 | Tara | 1 | 60000 | 2
SELECT e.name, m.name
FROM staff e
JOIN staff m ON e.mgr_id = m.id
WHERE e.id IN (4, 6)
ORDER BY e.id;
- Riya Qadir, Tara Pia
- Riya Om, Tara Om
- Qadir Riya, Pia Tara
- Riya Qadir, Tara Om
Answer: A. Riya Qadir, Tara Pia
Explanation
A self-join uses two aliases of the same table: Riya’s mgr_id is 3 (Qadir) and Tara’s is 2 (Pia).
Step-by-step solution
- The self-join matches each staff member with their manager through mgr_id.
- Riya (id 4) has mgr_id 3, which is Qadir; Tara (id 6) has mgr_id 2, which is Pia.
- Ordered by id, the answer is Riya Qadir, Tara Pia, option A.
Why the other options are wrong
- B. Riya's manager is Qadir (id 3), not Om.
- C. Option C swaps the employee and manager names.
- D. Tara's manager is Pia (id 2), not Om.
Tip: In a self-join, match the foreign key to the other alias's id.
Question 2Easy
Who is Pia’s manager according to this query?
staff
id | name | dept_id | salary | mgr_id
1 | Om | 1 | 90000 | NULL
2 | Pia | 1 | 60000 | 1
3 | Qadir | 2 | 75000 | 1
4 | Riya | 2 | 50000 | 3
5 | Sam | NULL | 40000 | 3
6 | Tara | 1 | 60000 | 2
SELECT m.name
FROM staff e JOIN staff m ON m.id = e.mgr_id
WHERE e.name = 'Pia';
- Qadir
- NULL
- Om
- Tara
Answer: C. Om
Explanation
Pia’s mgr_id is 1, and staff id 1 is Om.
Step-by-step solution
- Pia's row has mgr_id 1.
- The join matches staff m where m.id = 1.
- Staff id 1 is Om, so the answer is Om (option C).
Why the other options are wrong
- A. Qadir has id 3; he is not Pia's manager.
- B. NULL is Om's mgr_id, not Pia's, and a NULL name is not a manager.
- D. Tara has mgr_id 2, so Tara reports to Pia; she is not Pia's manager.
Tip: To find a manager's name, use the employee's mgr_id as the id to look up in the same table.
Question 3Easy
Table A has 4 rows and table B has 0 rows. How many rows does A CROSS JOIN B return?
- An error
- 0
- 4
- 1
Answer: B. 0
Explanation
A cross join returns |A| × |B| rows, and 4 × 0 = 0.
Step-by-step solution
- A cross join returns the rows of A multiplied by the rows of B.
- 4 × 0 = 0 rows.
- So the answer is option B, 0.
Why the other options are wrong
- A. An empty table is valid, and the cross join is simply empty.
- C. 4 would need B to have one row; with no rows, the product is 0.
- D. 1 is not the product of 4 and 0.
Tip: A cross join with an empty table returns no rows.
Question 4Easy
How many rows does this query return?
cities
id | name | state
1 | Mysuru | KA
2 | Hubballi | KA
3 | Nashik | MH
4 | Nagpur | MH
5 | Kochi | KL
SELECT a.name, b.name
FROM cities a CROSS JOIN cities b;
- 20
- 5
- 25
- 10
Answer: C. 25
Explanation
A table cross-joined with itself gives 5 × 5 = 25 rows, including each city paired with itself.
Step-by-step solution
- A CROSS JOIN pairs every row of cities with every row of cities.
- 5 rows × 5 rows = 25 pairs, including each city with itself.
- The answer is 25 (option C).
Why the other options are wrong
- A. 20 would leave out the 5 self-pairs; a cross join keeps them.
- B. 5 is the number of cities, not the number of pairs.
- D. 10 is the number of unordered pairs with different cities; a cross join keeps order and self-pairs.
Tip: Cross join row count = rows in A × rows in B.
Question 5Medium
Which names does this query return?
staff
id | name | dept_id | salary | mgr_id
1 | Om | 1 | 90000 | NULL
2 | Pia | 1 | 60000 | 1
3 | Qadir | 2 | 75000 | 1
4 | Riya | 2 | 50000 | 3
5 | Sam | NULL | 40000 | 3
6 | Tara | 1 | 60000 | 2
SELECT e.name
FROM staff e
JOIN staff m ON e.mgr_id = m.id
WHERE e.salary > m.salary;
- Qadir
- No rows
- Tara
- Om
Answer: B. No rows
Explanation
Compare each employee with their manager: Pia 60000 < 90000, Qadir 75000 < 90000, Riya 50000 < 75000, Sam 40000 < 75000, Tara 60000 = 60000 (not greater). Nobody earns more.
Step-by-step solution
- Match each employee to their manager through mgr_id.
- Pia 60k vs Om 90k, Qadir 75k vs Om 90k, Riya 50k vs Qadir 75k, Sam 40k vs Qadir 75k, Tara 60k vs Pia 60k.
- No employee earns strictly more than their manager.
Why the other options are wrong
- A. Qadir earns 75k, less than Om's 90k.
- C. Tara and Pia both earn 60k, so it is not greater.
- D. Om has no manager (NULL) so he is dropped by the inner join.
Tip: Self join = same table, two aliases.
Question 6Medium
Which query finds staff who earn more than their manager?
SELECT name FROM staff WHERE salary > mgr_id;SELECT e.name FROM staff e JOIN staff m ON e.mgr_id = m.id WHERE e.salary > m.salary;SELECT e.name FROM staff e JOIN staff m ON e.id = m.mgr_id WHERE e.salary > m.salary;SELECT e.name FROM staff e, staff m WHERE e.salary > m.salary;
Answer: B. SELECT e.name FROM staff e JOIN staff m ON e.mgr_id = m.id WHERE e.salary > m.salary;
Explanation
e is the employee and m the manager, linked by e.mgr_id = m.id. The second option reverses the roles and the last compares with everyone.
Step-by-step solution
- E is the employee row and m is the manager row.
- The join ON e.mgr_id = m.id links each employee to their manager.
- WHERE e.salary > m.salary keeps employees paid more than their manager (option B).
Why the other options are wrong
- A. It compares salary with mgr_id, a number that is not a salary, and does not join to the manager row.
- C. It joins e.id to m.mgr_id, which reverses the roles and finds the staff who manage e.
- D. It compares every pair of staff, not an employee with their own manager.
Tip: For manager questions, self-join with the employee's mgr_id = manager's id, then compare the two salary columns.
Question 7Medium
What does this query return?
staff
id | name | dept_id | salary | mgr_id
1 | Om | 1 | 90000 | NULL
2 | Pia | 1 | 60000 | 1
3 | Qadir | 2 | 75000 | 1
4 | Riya | 2 | 50000 | 3
5 | Sam | NULL | 40000 | 3
6 | Tara | 1 | 60000 | 2
SELECT m.name, COUNT(*) AS reports
FROM staff e
JOIN staff m ON e.mgr_id = m.id
GROUP BY m.name
ORDER BY reports DESC, m.name;
- Om 3, Qadir 2, Pia 1
- Om 2, Qadir 2, Pia 1, Tara 0
- Qadir 2, Om 2, Pia 1
- Om 2, Qadir 2, Pia 1
Answer: D. Om 2, Qadir 2, Pia 1
Explanation
Om manages Pia and Qadir, Qadir manages Riya and Sam, Pia manages Tara. Ties at 2 are ordered by name.
Step-by-step solution
- The join pairs each employee with their manager: Pia and Qadir report to Om, Riya and Sam to Qadir, and Tara to Pia.
- The counts are Om 2, Qadir 2 and Pia 1; Tara has no reports, so she is not listed.
- Ordered by reports descending then name, the answer is option D.
Why the other options are wrong
- A. Om has 2 reports, not 3, because Om's direct reports are Pia and Qadir.
- B. Tara has no reports, so she does not appear in an inner join.
- C. The order is wrong; on the tie Om comes before Qadir alphabetically.
Tip: Count reports with a self-join grouped by the manager's name.
Question 8Medium
What is the first row returned?
sizes
s
S
M
L
colours
c
Red
Blue
SELECT s, c
FROM sizes CROSS JOIN colours
ORDER BY c, s;
- L, Red
- S, Blue
- S, Red
- L, Blue
Answer: D. L, Blue
Explanation
All 6 combinations are produced, then sorted by colour (Blue first) and then size alphabetically (L < M < S).
Step-by-step solution
- CROSS JOIN makes all 6 combinations of size and colour.
- ORDER BY c, s sorts by colour first, so Blue comes before Red.
- Within Blue, the sizes sort alphabetically as L, M, S, so the first row is L, Blue, option D.
Why the other options are wrong
- A. Red sorts after Blue, so L, Red is not the first row.
- B. S is the last size alphabetically, so S, Blue is not first.
- C. S, Red is a late row, not the first.
Tip: ORDER BY sorts text alphabetically: L comes before M, and M before S.
Question 9Hard
How many rows does this query return?
teams
name
Lions
Tigers
Bears
Wolves
SELECT a.name, b.name
FROM teams a
JOIN teams b ON a.name < b.name;
- 6
- 12
- 16
- 4
Answer: A. 6
Explanation
Using < instead of <> lists each unordered pair once: C(4, 2) = 6 fixtures.
Step-by-step solution
- The condition a.name less than b.name keeps each unordered pair once.
- With 4 teams that is 4 × 3 / 2 = 6 pairs: Lions-Tigers, Lions-Wolves, Tigers-Wolves, Bears-Lions, Bears-Tigers and Bears-Wolves.
- So the answer is option A, 6 rows.
Why the other options are wrong
- B. 12 would be every ordered pair with different names, but the less-than test keeps each pair once.
- C. 16 is 4 × 4, which includes self-pairs that the test skips.
- D. 4 is too few; there are 6 pairs.
Tip: A less-than test on the same table keeps each unordered pair once: n × (n minus 1) / 2 rows.
Question 10Hard
A CROSS JOIN between tables of 1,000 and 2,000 rows is filtered by WHERE a.id = b.a_id. How does the result compare with a INNER JOIN b ON a.id = b.a_id?
- Logically identical results; optimizers usually execute both the same way
- The INNER JOIN also returns unmatched rows
- The CROSS JOIN version returns 2,000,000 rows
- The CROSS JOIN version ignores the WHERE clause
Answer: A. Logically identical results; optimizers usually execute both the same way
Explanation
An inner join is defined as a filtered cross product, so the results are the same; explicit JOIN ... ON is just clearer.
Step-by-step solution
- A CROSS JOIN gives every pairing, then WHERE a.id = b.a_id keeps only the matching pairs.
- INNER JOIN ON a.id = b.a_id keeps the same matching pairs directly.
- So both give the same rows; the optimizer can run either plan (option A).
Why the other options are wrong
- B. Neither version keeps unmatched rows, so the INNER JOIN does not return extra rows.
- C. 2,000,000 is the size of the full cross product before the filter, not the filtered result.
- D. The WHERE clause is applied to the cross join, so it is not ignored.
Tip: CROSS JOIN with a WHERE equality is an inner join written the old way; prefer JOIN ... ON for clarity.
Practise all 21 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 →