Skip to content
Advertisement

How do I do nested if statements in SQL?

Write a program that will check the answers for the latest test. The program will be given a table answers with the following columns:

id – the unique ID of the question;

correct_answer – the correct answer to the question, given as a string;

given_answer – the answer given to the question, which can be NULL.

Your task is to return the table with a column id and a column checks, where for each answers id the following string should be returned:

"no answer" if the given_answer is empty;

"correct" if the given_answer is the same as the correct_answer;

"incorrect" if the given_answer is not empty and is incorrect.

Order the records in the answer table by id.

CREATE PROCEDURE testCheck()
    SELECT id, IF (...) AS checks
    FROM answers
    ORDER BY id;

enter image description here

Advertisement

Answer

Use CASE rather than IF when there are multiple conditions.

SELECT id,
    CASE WHEN given_answer IS NULL THEN 'no answer'
         WHEN given_answer = correct_answer THEN 'correct'
         ELSE 'incorrect'
    END as checks
FROM answers
ORDER BY id
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement