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 COUNT(*) FROM staff
WHERE EXISTS (SELECT 1 FROM staff WHERE salary > 100000);
- 6
- NULL
- 1
- 0
Answer: D. 0
Explanation
The subquery is not correlated and finds no salary above 100000, so EXISTS is FALSE for every row and COUNT(*) is 0.
Step-by-step solution
- The subquery looks for salary > 100000.
- No such row, so EXISTS is false for every outer row.
- No rows pass WHERE, so COUNT(*) is 0.
Why the other options are wrong
- A. EXISTS is false so no row is counted.
- B. COUNT returns 0, not NULL.
- C. Nothing satisfies the condition.
Tip: COUNT of nothing is 0.
Question 2Easy
Which item names does this query return?
items
id | name | cat | price
1 | Rice | Food | 60
2 | Soap | Home | 40
3 | Tea | Food | 120
4 | Mop | Home | 250
5 | Oil | Food | 180
orders
oid | item_id | qty
101 | 1 | 2
102 | 3 | 1
103 | 1 | 5
104 | NULL | 3
105 | 5 | 1
SELECT name FROM items
WHERE id IN (SELECT item_id FROM orders)
ORDER BY id;
- Rice, Rice, Tea, Oil
- Rice, Tea, Oil
- Rice, Tea, Oil, Soap
- Tea, Oil
Answer: B. Rice, Tea, Oil
Explanation
IN checks membership, so Rice appears once even though it was ordered twice; the NULL item_id matches nothing.
Step-by-step solution
- Order item_ids: 1, 3, 1, NULL and 5.
- IN checks each item once against the set {1, 3, 5}; the NULL matches nothing.
- Items 1 Rice, 3 Tea and 5 Oil match, listed by id: Rice, Tea, Oil (option B).
Why the other options are wrong
- A. IN checks membership, so Rice appears once, not twice.
- C. Soap (id 2) was never ordered, so it is not in the IN list.
- D. Rice was ordered twice, so it is included.
Tip: IN is a membership test: each matching item appears once, however many orders it has.
Question 3Easy
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 name FROM staff
WHERE salary > (SELECT AVG(salary) FROM staff)
ORDER BY name;
- Om
- Om, Pia, Qadir, Tara
- Qadir
- Om, Qadir
Answer: D. Om, Qadir
Explanation
The average is (90000 + 60000 + 75000 + 50000 + 40000 + 60000) / 6 = 62500, so only 90000 and 75000 are above it.
Step-by-step solution
- Average salary: (90000 + 60000 + 75000 + 50000 + 40000 + 60000) ÷ 6 = 375000 ÷ 6 = 62500.
- Salaries above 62500: Om 90000 and Qadir 75000.
- Sorted by name the answer is Om, Qadir (option D).
Why the other options are wrong
- A. Om alone misses Qadir, whose 75000 is above 62500.
- B. Pia and Tara earn 60000, which is below 62500, so they are not returned.
- C. Qadir alone misses Om, whose 90000 is above 62500.
Tip: Compute the average first; a single-value subquery in WHERE compares every row to that one number.
Question 4Easy
What makes a subquery correlated?
- A subquery that refers to a column of the outer query, so it is evaluated for each outer row
- A subquery joined with UNION
- A subquery that returns exactly one value
- A subquery in the FROM clause
Answer: A. A subquery that refers to a column of the outer query, so it is evaluated for each outer row
Explanation
Because it depends on the current outer row, a correlated subquery is logically re-run per row (optimizers may rewrite it as a join).
Step-by-step solution
- A correlated subquery uses a column from the outer query.
- So its result depends on the current outer row.
- It is logically re-evaluated for each outer row.
Why the other options are wrong
- B. UNION has nothing to do with correlation.
- C. A scalar subquery returns one value but may be uncorrelated.
- D. A subquery in FROM is a derived table.
Tip: Inner uses outer = correlated.
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 name FROM staff s
WHERE salary > (SELECT AVG(salary) FROM staff WHERE dept_id = s.dept_id)
ORDER BY name;
- Om
- Om, Qadir
- Om, Pia, Qadir
- Om, Qadir, Sam
Answer: B. Om, Qadir
Explanation
Correlated subquery: IT average is 70000 (Om above), Finance average is 62500 (Qadir above). For Sam, dept_id = NULL matches nothing, so the average is NULL and he is excluded.
Step-by-step solution
- Each row is compared to the average of its own department.
- IT (dept 1): Om 90000, Pia 60000, Tara 60000, average 70000. Only Om is above.
- Finance (dept 2): Qadir 75000, Riya 50000, average 62500. Only Qadir is above. Sam has NULL dept, so his average is NULL and he is excluded. Answer: Om, Qadir (option B).
Why the other options are wrong
- A. Om alone misses Qadir, who is above the Finance average of 62500.
- C. Pia earns 60000, which is below the IT average of 70000.
- D. Sam's dept_id is NULL, so the subquery average is NULL and the comparison is never TRUE.
Tip: A correlated subquery runs once per row; a NULL key matches no rows, so its average is NULL.
Question 6Medium
What happens when this query runs (SQLite)?
items
id | name | cat | price
1 | Rice | Food | 60
2 | Soap | Home | 40
3 | Tea | Food | 120
4 | Mop | Home | 250
5 | Oil | Food | 180
orders
oid | item_id | qty
101 | 1 | 2
102 | 3 | 1
103 | 1 | 5
104 | NULL | 3
105 | 5 | 1
SELECT name FROM items
WHERE price = (SELECT price FROM items WHERE cat = 'Food');
- It returns no rows
- It always raises an error
- It returns Rice; SQLite uses the first value the subquery produces
- It returns Rice, Tea and Oil
Answer: C. It returns Rice; SQLite uses the first value the subquery produces
Explanation
SQLite silently takes the first row of a multi-row scalar subquery (here 60). Standard SQL, MySQL and PostgreSQL raise "subquery returns more than one row" instead.
Step-by-step solution
- The subquery returns the prices of Food items in order: 60 (Rice), 120 (Tea), 180 (Oil).
- SQLite does not raise an error for this; it uses the first row produced, 60.
- Only Rice has price 60, so the query returns Rice (option C).
Why the other options are wrong
- A. Rice has price 60, which matches the first subquery value, so the result is not empty.
- B. SQLite does not raise an error for multi-row scalar subqueries; other databases do.
- D. Tea (120) and Oil (180) do not match 60, so they are not returned.
Tip: Multi-row scalar subqueries are an error in most databases but silently take the first row in SQLite; add LIMIT 1 or an aggregate to be explicit.
Question 7Medium
In standard SQL / MySQL, what does x = ANY (subquery) mean?
- x equals every value of the subquery
- It is equivalent to x IN (subquery)
- x equals the first value only
- x is not in the subquery
Answer: B. It is equivalent to x IN (subquery)
Explanation
= ANY is TRUE if x equals at least one returned value, exactly like IN. <> ALL is the equivalent of NOT IN.
Step-by-step solution
- = ANY returns true if x equals at least one subquery value.
- That is the same as IN.
Why the other options are wrong
- A. Equal to every value is = ALL.
- C. ANY is not limited to the first value.
- D. Not in is <> ALL or NOT IN.
Tip: = ANY means IN.
Question 8Medium
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 MAX(salary) FROM staff
WHERE salary < (SELECT MAX(salary) FROM staff);
- 75000
- 60000
- NULL
- 90000
Answer: A. 75000
Explanation
This is the classic second-highest pattern: the largest salary strictly below the maximum 90000 is 75000.
Step-by-step solution
- The inner subquery gives the maximum salary, 90000.
- The outer query keeps salaries strictly below 90000: 60000, 75000, 50000, 40000, 60000.
- The largest of those is 75000 (option A).
Why the other options are wrong
- B. 60000 is a salary below the maximum but not the largest such value.
- C. MAX returns NULL only when no rows match; here 75000 matches.
- D. 90000 is the maximum itself, and the strict < excludes it.
Tip: Second-highest pattern: MAX(x) WHERE x < (SELECT MAX(x)); it ignores ties with the top value.
Question 9Hard
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 DISTINCT student FROM enrollments e
WHERE NOT EXISTS (
SELECT 1 FROM enrollments x
WHERE x.course IN ('DBMS', 'OS')
AND NOT EXISTS (SELECT 1 FROM enrollments y
WHERE y.student = e.student AND y.course = x.course)
);
- 4
- 1
- 3
- 2
Answer: D. 2
Explanation
Double NOT EXISTS expresses "no required course that the student lacks": Ira and Kavi take both DBMS and OS.
Step-by-step solution
- The inner NOT EXISTS looks for a required course (DBMS or OS) that student e does not take.
- The outer NOT EXISTS keeps e only when no required course is missing.
- Ira and Kavi take both courses; Jay misses OS; Lia misses both. The count is 2 (option D).
Why the other options are wrong
- A. Four is the number of distinct students, but Jay and Lia lack a required course.
- B. Only one student is not the result; both Ira and Kavi qualify.
- C. Three would include Jay, who has DBMS but not OS.
Tip: Double NOT EXISTS expresses 'for all' (every required course is taken).
Question 10Hard
What does this query return?
items
id | name | cat | price
1 | Rice | Food | 60
2 | Soap | Home | 40
3 | Tea | Food | 120
4 | Mop | Home | 250
5 | Oil | Food | 180
orders
oid | item_id | qty
101 | 1 | 2
102 | 3 | 1
103 | 1 | 5
104 | NULL | 3
105 | 5 | 1
SELECT cat FROM items
GROUP BY cat
HAVING SUM(price) > (SELECT SUM(price) / 2 FROM items);
- Food
- No rows
- Food, Home
- Home
Answer: A. Food
Explanation
The overall total is 650, so the threshold is 650 / 2 = 325. Food sums to 360 (above), Home to 290 (below).
Step-by-step solution
- Totals per category: Food 60 + 120 + 180 = 360; Home 40 + 250 = 290.
- The subquery computes the whole table's SUM(price) ÷ 2 = 650 ÷ 2 = 325.
- Only Food (360) is above 325, so the answer is Food (option A).
Why the other options are wrong
- B. Food's total of 360 is above 325, so Food does return.
- C. Home's total of 290 is below 325, so it is excluded.
- D. Home's total of 290 is below 325, and Food is the one that passes.
Tip: HAVING filters groups by aggregate; compare group totals with a threshold from an uncorrelated subquery.
Practise all 25 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 →