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
.
x
CREATE PROCEDURE testCheck()
SELECT id, IF ( ) AS checks
FROM answers
ORDER BY id;
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