SQL Interview Questions – Evaluate Boolean Expression

Spread the love

Problem Description –

Write an SQL query to evaluate the boolean expressions in Expressions table.

Return the result table in any order.

The query result format is in the following example.

Difficulty Level – Medium

Problem Link – Boolean Expression

Solution –

SELECT
    left_operand,
    operator,
    right_operand,
    CASE WHEN operator = '<' AND v1.value < v2.value THEN 'true'
		WHEN operator = '=' AND v1.value = v2.value THEN 'true'
        WHEN operator = '>' AND v1.value > v2.value THEN 'true'
        ELSE 'false' 
	END as value
FROM Expressions as e
JOIN Variables as v1
ON e.left_operand = v1.name
JOIN Variables as v2
ON e.right_operand = v2.name

Rating: 1 out of 5.

Leave a Reply