Test Check
Description
Your professor gave the class a bonus task: 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 thegiven_answeris empty;"correct"if thegiven_answeris the same as thecorrect_answer;"incorrect"if thegiven_answeris not empty and is incorrect.
Order the records in the answer table by id.
Example
For given table answers
| id | correct_answer | given_answer |
|---|---|---|
| 1 | a | a |
| 2 | b | NULL |
| 3 | c | b |
the output should be
| id | checks |
|---|---|
| 1 | correct |
| 2 | no answer |
| 3 | incorrect |
- [execution time limit] 10 seconds (mysql)
Solution
1
2
3
4
CREATE PROCEDURE testCheck()
SELECT id, IF ( given_answer IS NULL, 'no answer', IF ( given_answer =correct_answer, 'correct', 'incorrect') ) AS checks
FROM answers
ORDER BY id;