Question: Using CASE

Check your understanding of CASE for handling logical operations in SQL.

Question

Imagine you’re in charge of a traffic management system. Your task is to write an SQL query using a session variable and a CASE statement to evaluate the action drivers should take based on the color of a traffic light.

Start by setting the variable SET @color = 'green'; to simulate different traffic light colors, like ‘green,’ ‘yellow,’ or ‘red.’ Your query should return two columns: Color (the current traffic light color) and TrafficAction (which should be ‘Go’ for green, ‘Caution’ for yellow, and ‘Stop’ for red).

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.