SQL Functions & CASE · Question 8 · Medium

SQL Functions & CASE question 8

Table Student:
roll | name | marks
1 | Aarav | 82
2 | Diya | 67
3 | Kabir | 45
4 | Meher | 91
5 | Rohan | 30
6 | Sneha | NULL

A teacher writes the grading rule below. What does the query return?

SELECT name,
       CASE WHEN marks >= 40 THEN 'C'
            WHEN marks >= 60 THEN 'B'
            WHEN marks >= 80 THEN 'A'
            ELSE 'F' END
FROM Student
WHERE roll IN (4, 5)
ORDER BY roll;
  1. Meher, A; Rohan, F
  2. Meher, C; Rohan, F
  3. Meher, A; Rohan, C
  4. Meher, B; Rohan, F
Answer and explanation

Answer: B. Meher, C; Rohan, F

Explanation: CASE checks its WHEN conditions from top to bottom and stops at the first one that is true. Meher's 91 already satisfies marks >= 40, so she gets C and the later branches are never reached. The stricter conditions must be written first.

Previous: What does this query return? · Next: Table Student: roll | name | marks 1 | Aarav | 82 2 | Diya | 67 3 | Kabir | 45…

Practise sql functions & case

Try a fresh set of sql functions & case questions with an explanation after every answer, free and without signing up: SQL Functions & CASE practice. Or sit a full placement mock test.

More sql functions & case questions

More sql practice

UNION, INTERSECT & EXCEPT · Tables, Keys & Constraints · DML & Transactions · SELECT, WHERE & ORDER BY · SQL Joins · Aggregates & GROUP BY · Subqueries & CTEs · Window Functions

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge