Answer: Using CASE
Find a detailed explanation of CASE for handling logical operations in SQL.
Solution
The solution is given below:
Code explanation
The explanation of the solution code is given below:
Line 2: The
SETcommand assigns the string value'green'to the variable@color. This variable can be used later in the query to reference the color.Line 4: The
SELECTquery selects the value of the variable@color. We useASto set the aliases for the columns.Lines 5–10: The
CASEstatement evaluates the value of@colorto determine the traffic action. It checks if@coloris equal to'green', it returns'Go'; if it is equal to'yellow', it returns'Caution'; if it is equal to'red', it returns'Stop'. If@colormatches none of these conditions, it returns'Unknown'. The statement concludes withEND...