Sample questions
Question 1Easy
What does this query return?
sales
id | region | rep | amount
1 | North | Ajay | 500
2 | North | Bina | 300
3 | South | Ajay | 200
4 | South | Chetan | NULL
5 | East | Bina | 700
6 | North | Ajay | 100
7 | South | Chetan | 400
SELECT MAX(amount) - MIN(amount) FROM sales;
- NULL
- 700
- 500
- 600
Answer: D. 600
Explanation
MAX is 700 and MIN is 100 (the NULL is ignored), so the difference is 600.
Step-by-step solution
- MAX(amount) is 700 and MIN(amount) is 100, because the NULL is ignored.
- The difference is 700 - 100 = 600.
- So the answer is option D, 600.
Why the other options are wrong
- A. MAX and MIN ignore NULL, so the result is not NULL.
- B. 700 is MAX alone, not the difference.
- C. 500 is the amount of the first row, not the result.
Tip: MAX and MIN skip NULL values.
Question 2Easy
A table has 10 rows; the column bonus is NULL in 4 of them. What is COUNT(*) - COUNT(bonus)?
- 4
- 10
- 0
- 6
Answer: A. 4
Explanation
COUNT(*) is 10 and COUNT(bonus) counts only the 6 non-NULL values, so the difference is the number of NULLs.
Step-by-step solution
- COUNT(*) counts all 10 rows.
- COUNT(bonus) counts only non-NULL values: 6.
- 10 - 6 = 4, the number of NULLs.
Why the other options are wrong
- B. COUNT(bonus) is not zero.
- C. The counts differ because of NULLs.
- D. That is COUNT(bonus), not the difference.
Tip: COUNT(*) - COUNT(col) = NULLs.
Question 3Easy
A query must keep only regions whose total sales exceed 800. In which clause does the condition SUM(amount) > 800 belong?
- HAVING
- WHERE
- ORDER BY
- DISTINCT
Answer: A. HAVING
Explanation
WHERE filters individual rows before grouping; HAVING filters whole groups and may use aggregate functions.
Step-by-step solution
- WHERE filters single rows before grouping, so it cannot test a total.
- HAVING filters groups after aggregation and may use SUM(amount).
- So the condition belongs in HAVING, option A.
Why the other options are wrong
- B. WHERE cannot use an aggregate like SUM(amount) because groups do not exist yet.
- C. ORDER BY only sorts the result; it does not filter rows.
- D. DISTINCT removes duplicate rows; it does not filter groups.
Tip: Row conditions go in WHERE; conditions on totals or counts go in HAVING.
Question 4Easy
What does this query return?
trainees
id | name | batch | score | joined
1 | Pooja | B1 | 82 | 2024-01-15
2 | Manish | B2 | 67 | 2024-03-02
3 | Lata | B1 | NULL | 2024-01-15
4 | Imran | B3 | 91 | 2024-07-20
5 | Gita | B2 | 74 | 2024-03-02
6 | Farhan | B1 | 58 | 2024-02-10
SELECT COUNT(*) FROM trainees WHERE score > 70;
- 4
- 6
- 2
- 3
Answer: D. 3
Explanation
Scores above 70 are 82, 91 and 74. The NULL score does not satisfy the condition.
Step-by-step solution
- The condition is score greater than 70.
- Pooja (82), Imran (91) and Gita (74) pass; Manish (67) and Farhan (58) fail.
- The NULL score for Lata is not TRUE, so the count is 3, option D.
Why the other options are wrong
- A. 4 would count a row that fails the test, such as Manish with 67.
- B. 6 is the number of rows, but only some pass.
- C. 2 leaves out Gita (74), who passes.
Tip: A NULL in a comparison is not TRUE, so the row is not counted.
Question 5Medium
What happens when this query runs (SQLite)?
sales
id | region | rep | amount
1 | North | Ajay | 500
2 | North | Bina | 300
3 | South | Ajay | 200
4 | South | Chetan | NULL
5 | East | Bina | 700
6 | North | Ajay | 100
7 | South | Chetan | 400
SELECT region FROM sales
WHERE SUM(amount) > 500
GROUP BY region;
- Returns all three regions
- Error: an aggregate function is not allowed in WHERE
- Returns North and South
- Returns North only
Answer: B. Error: an aggregate function is not allowed in WHERE
Explanation
WHERE runs before groups exist, so aggregates cannot appear there. Move the condition to HAVING.
Step-by-step solution
- WHERE runs before grouping, so aggregate functions are not allowed there.
- SUM(amount) inside WHERE causes an error in SQLite.
- So the query fails with an error, option B.
Why the other options are wrong
- A. The query does not run, so no regions are returned.
- C. The query fails before any rows are returned.
- D. The query does not run, so no single region is returned.
Tip: Move aggregate conditions from WHERE to HAVING.
Question 6Medium
What does this query return?
sales
id | region | rep | amount
1 | North | Ajay | 500
2 | North | Bina | 300
3 | South | Ajay | 200
4 | South | Chetan | NULL
5 | East | Bina | 700
6 | North | Ajay | 100
7 | South | Chetan | 400
SELECT rep, COUNT(*) AS n
FROM sales
GROUP BY rep
ORDER BY n DESC, rep
LIMIT 1;
- Ajay 2
- Chetan 2
- Ajay 3
- Bina 2
Answer: C. Ajay 3
Explanation
Ajay has 3 sales, Bina 2 and Chetan 2, so Ajay is first in descending count order.
Step-by-step solution
- Group by rep: Ajay (ids 1,3,6) = 3,
Bina = 2,
Chetan = 2.
COUNT(*) counts rows even if amount is NULL.- Highest count first,
LIMIT 1: Ajay 3.
Why the other options are wrong
- A. Ajay has three sales, not two.
- B. Chetan has two, which is fewer than Ajay.
- D. Bina has two sales, fewer than Ajay.
Tip: COUNT(*) ignores NULL-ness.
Question 7Medium
Which batches does this query return?
trainees
id | name | batch | score | joined
1 | Pooja | B1 | 82 | 2024-01-15
2 | Manish | B2 | 67 | 2024-03-02
3 | Lata | B1 | NULL | 2024-01-15
4 | Imran | B3 | 91 | 2024-07-20
5 | Gita | B2 | 74 | 2024-03-02
6 | Farhan | B1 | 58 | 2024-02-10
SELECT batch FROM trainees
GROUP BY batch
HAVING MIN(score) >= 60
ORDER BY batch;
- B1, B2, B3
- B1, B3
- B2, B3
- B3
Answer: C. B2, B3
Explanation
MIN ignores NULL: B1’s minimum is 58 (Farhan), which fails; B2 (67) and B3 (91) pass.
Step-by-step solution
- MIN ignores NULL, so B1's scores are 82 and 58, with minimum 58.
- B1 fails the test of at least 60.
- B2 (minimum 67) and B3 (minimum 91) pass, so the answer is option C.
Why the other options are wrong
- A. B1 has a minimum of 58, so it fails.
- B. B2 has a minimum of 67, so it passes and must be included.
- D. B2 also passes, so it must be included.
Tip: MIN and MAX ignore NULL, so a NULL does not hide a low value that is present.
Question 8Medium
What does this query return?
trainees
id | name | batch | score | joined
1 | Pooja | B1 | 82 | 2024-01-15
2 | Manish | B2 | 67 | 2024-03-02
3 | Lata | B1 | NULL | 2024-01-15
4 | Imran | B3 | 91 | 2024-07-20
5 | Gita | B2 | 74 | 2024-03-02
6 | Farhan | B1 | 58 | 2024-02-10
SELECT batch, COUNT(*), COUNT(score)
FROM trainees
GROUP BY batch
ORDER BY batch;
- B1 2 2, B2 2 2, B3 1 1
- B1 3 2, B2 2 2, B3 1 1
- B1 3 3, B2 2 2, B3 1 1
- B1 3 2, B2 2 2
Answer: B. B1 3 2, B2 2 2, B3 1 1
Explanation
B1 has three trainees but Lata has a NULL score, so COUNT(score) is 2 there.
Step-by-step solution
- COUNT(*) counts all rows in each batch.
- COUNT(score) skips Lata's NULL, so B1 has 2 scores.
- B1 has 3 rows and 2 scores, B2 has 2 and 2, B3 has 1 and 1, so the answer is option B.
Why the other options are wrong
- A. B1 has 3 rows, including Lata, so COUNT(*) is 3.
- C. B1 has 2 non-NULL scores, not 3.
- D. B3 is also in the result, with 1 row and 1 score.
Tip: Compare COUNT(*) with COUNT(column) to spot NULL values in a group.
Question 9Hard
What does this query return?
trainees
id | name | batch | score | joined
1 | Pooja | B1 | 82 | 2024-01-15
2 | Manish | B2 | 67 | 2024-03-02
3 | Lata | B1 | NULL | 2024-01-15
4 | Imran | B3 | 91 | 2024-07-20
5 | Gita | B2 | 74 | 2024-03-02
6 | Farhan | B1 | 58 | 2024-02-10
SELECT joined, GROUP_CONCAT(name, '+')
FROM (SELECT * FROM trainees ORDER BY id)
GROUP BY joined
HAVING COUNT(*) > 1
ORDER BY joined;
(SQLite GROUP_CONCAT; MySQL has GROUP_CONCAT with SEPARATOR, standard SQL has LISTAGG/STRING_AGG.)
- 2024-03-02 Manish+Gita
- All four dates with one name each
- 2024-01-15 Pooja+Lata, 2024-03-02 Manish+Gita
- 2024-01-15 Pooja+Lata
Answer: C. 2024-01-15 Pooja+Lata, 2024-03-02 Manish+Gita
Explanation
Two joining dates are shared by two trainees each; GROUP_CONCAT joins the names of each group with the given separator.
Step-by-step solution
- The subquery orders rows by id, so names are concatenated in id order.
- Two joining dates are shared: 2024-01-15 (Pooja, Lata) and 2024-03-02 (Manish, Gita).
- Those two groups each have 2 rows, so the answer is option C.
Why the other options are wrong
- A. 2024-01-15 also has two trainees, so it must appear.
- B. Only the dates with more than one trainee appear, not all four.
- D. 2024-03-02 also has two trainees, so it must appear.
Tip: HAVING COUNT(*) greater than 1 keeps only groups with repeated values.
Question 10Hard
What does this query return?
sales
id | region | rep | amount
1 | North | Ajay | 500
2 | North | Bina | 300
3 | South | Ajay | 200
4 | South | Chetan | NULL
5 | East | Bina | 700
6 | North | Ajay | 100
7 | South | Chetan | 400
SELECT SUM(CASE WHEN amount >= 300 THEN 1 ELSE 0 END) AS big,
SUM(CASE WHEN amount < 300 THEN 1 ELSE 0 END) AS small
FROM sales;
- 5, 2
- 3, 3
- 4, 3
- 4, 2
Answer: D. 4, 2
Explanation
Amounts 500, 300, 700, 400 are >= 300 (4); 200 and 100 are below (2). The NULL row is neither, so it goes to ELSE 0 in both.
Step-by-step solution
- Amounts 500, 300, 700 and 400 are at least 300, so big is 4.
- Amounts 200 and 100 are below 300, so small is 2.
- The NULL row fails both tests and goes to ELSE 0, so the answer is option D, 4 and 2.
Why the other options are wrong
- A. 5 big would need the 200 row, which is below 300.
- B. 3 big would miss one row of 300 or more.
- C. Small is 2, not 3; the NULL goes to ELSE 0, not small.
Tip: A CASE with ELSE 0 inside SUM counts matching rows; NULL fails >= and < and falls to ELSE.
Practise all 24 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 →