SQL Functions & CASE questions and answers

CASE WHEN, conditional aggregation, string and numeric functions. Practise sql functions & case questions from the sql section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.

Sample sql functions & case questions with worked answers

Question 1

A CASE expression has no ELSE part, and for some row none of its WHEN conditions is true. What does CASE return for that row?

  1. NULL
  2. 0
  3. An empty string
  4. An error is raised and the query stops

Answer: A. NULL

Explanation: A missing ELSE behaves like ELSE NULL. The row is still returned, with NULL in that column.

Question 2

Which statement about the SQL CASE expression is correct?

  1. It can be written only in the SELECT list
  2. It produces a value, so it can be used in SELECT, WHERE, ORDER BY and inside aggregate functions
  3. Every CASE must have an ELSE part, or the query does not run
  4. All WHEN conditions are evaluated and the last true one decides the result

Answer: B. It produces a value, so it can be used in SELECT, WHERE, ORDER BY and inside aggregate functions

Explanation: CASE is an expression, so it is allowed wherever a value is allowed. ELSE is optional (it defaults to NULL), and the first true WHEN decides the result, not the last.

Question 3

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

This query uses the simple form of CASE (CASE marks WHEN ...). What does it return?

SELECT name,
       CASE marks WHEN NULL THEN 'Absent' ELSE 'Present' END
FROM Student
WHERE roll = 6;
  1. Sneha, Absent
  2. Sneha, NULL
  3. No rows
  4. Sneha, Present

Answer: D. Sneha, Present

Explanation: CASE marks WHEN NULL compares with an equals test, marks = NULL, which is UNKNOWN even when marks is NULL. The branch never matches, so ELSE gives Present. To catch NULL, write CASE WHEN marks IS NULL THEN ...

All 20 sql functions & case questions with answers

SQL Functions & CASE: common questions

What are sql functions & case questions?

CASE WHEN, conditional aggregation, string and numeric functions. They appear in the sql section of many placement tests.

Where can I practise sql functions & case questions with answers for free?

Right here on LogicWiz Exams. Every sql functions & case question has a worked explanation after you answer. Practice is free and needs no sign-up.

How should I practise sql functions & case?

Start with easy questions, then move to medium and hard. Read the explanation after each answer, and come back to the ones you missed.

Start practising

On this page you can start a fresh set of sql functions & case questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: SQL practice.

More sql topics

Other subjects

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge · Test from a job description · LogicWiz Open: free team quiz · Free interview prep