Evaluate stored comparison expressions against a table of variable values
Company: Point72
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Evaluate each row of `Expressions` using the variable values stored in `Variables`. Each expression compares two variables by name with one of the operators `<`, `>` or `=`. Return all expression columns plus a `value` column holding the string `'true'` when the comparison holds and `'false'` when it does not.
```hint Resolve the names first
An expression row only holds variable names. Work out how to get a number for each of its two operands before you think about the operator.
```
### Input Table(s)
`Variables` (primary key `name`)
| Column | Type | Meaning |
|---|---|---|
| name | VARCHAR(10) | Variable name, primary key |
| value | INT | Integer value of the variable, may be NULL |
`Expressions` (primary key `(left_operand, operator, right_operand)`)
| Column | Type | Meaning |
|---|---|---|
| left_operand | VARCHAR(10) | Name of the variable on the left side, references `Variables.name` |
| operator | ENUM('<', '>', '=') | Comparison to apply |
| right_operand | VARCHAR(10) | Name of the variable on the right side, references `Variables.name` |
### Output Contract
- Columns, in this order: `left_operand`, `operator`, `right_operand`, `value`.
- `value` is text, exactly `'true'` or `'false'` in lowercase.
- One row per row of `Expressions`.
- Rows ordered by `left_operand` ascending, then `operator` in the fixed order `'<'`, `'='`, `'>'`, then `right_operand` ascending. The operator order is the same whether the column is stored as an enum or as text, so do not rely on the column's own sort order. Together these columns are the primary key, so the order is unique.
- No rounding applies.
### Example
`Variables`
| name | value |
|---|---|
| a | 3 |
| b | 7 |
| c | 3 |
`Expressions`
| left_operand | operator | right_operand |
|---|---|---|
| a | < | b |
| a | = | c |
| b | < | a |
| b | = | b |
| b | > | c |
| c | < | a |
| c | = | b |
Expected result
| left_operand | operator | right_operand | value |
|---|---|---|---|
| a | < | b | true |
| a | = | c | true |
| b | < | a | false |
| b | = | b | true |
| b | > | c | true |
| c | < | a | false |
| c | = | b | false |
With a = 3, b = 7 and c = 3: 3 < 7 and 3 = 3 hold, 7 < 3 does not, 7 = 7 holds, 7 > 3 holds, and 3 < 3 and 3 = 7 do not. The comparison `c < a` is false because the values are equal and `<` is strict. The three `b` rows show the operator order: `<`, then `=`, then `>`.
### Constraints and Clarifications
- `operator` is one of `'<'`, `'>'`, `'='`.
- Both operands of every expression are guaranteed to exist in `Variables`.
- `Expressions` has no duplicate rows, since its three columns form the primary key. The same variable can appear in many expressions, and an expression may compare a variable with itself.
- `Variables.value` may be NULL. When either operand's value is NULL, the comparison is unknown and `value` is `'false'`.
Overview: Write a SQL query that evaluates stored comparison expressions such as a < b by looking up each variable's value in a separate table and returning true or false as text. The problem tests joining the same lookup table twice, CASE expressions and how comparisons behave with NULL values.