Quick 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.

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.

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. Tables: - `variables(name VARCHAR(10) PRIMARY KEY, value INTEGER)`: `name` is the variable name and `value` is its integer value, which may be NULL. - `expressions(left_operand VARCHAR(10), operator VARCHAR(1), right_operand VARCHAR(10))`, primary key `(left_operand, operator, right_operand)`: `left_operand` names the variable on the left side and `right_operand` names the variable on the right side, both referencing `variables.name`; `operator` is the comparison to apply, one of '<', '>', '=', stored as text. Output: - 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`. - Order rows 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. Rules: - Both operands of every expression exist in `variables`. - `expressions` has no duplicate rows. 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'. Example: with a = 3, b = 7 and c = 3, the expression (a, '<', b) gives 'true', (b, '=', b) gives 'true', and (c, '<', a) gives 'false' because the values are equal and '<' is strict.

Tables

variables(name VARCHAR(10), value INTEGER)

expressions(left_operand VARCHAR(10), operator VARCHAR(1), right_operand VARCHAR(10))

Hints

  1. 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.
  2. A comparison with a NULL operand is unknown in SQL. The output still needs the string 'false' for those rows, so check which branch an unknown result falls into.
Point72Data ScientisteasyData Manipulation (SQL/Python)

Evaluate Boolean Expressions

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.

Tables:

  • variables(name VARCHAR(10) PRIMARY KEY, value INTEGER): name is the variable name and value is its integer value, which may be NULL.
  • expressions(left_operand VARCHAR(10), operator VARCHAR(1), right_operand VARCHAR(10)), primary key (left_operand, operator, right_operand): left_operand names the variable on the left side and right_operand names the variable on the right side, both referencing variables.name; operator is the comparison to apply, one of '<', '>', '=', stored as text.

Output:

  • 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.
  • Order rows 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.

Rules:

  • Both operands of every expression exist in variables.
  • expressions has no duplicate rows. 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'.

Example: with a = 3, b = 7 and c = 3, the expression (a, '<', b) gives 'true', (b, '=', b) gives 'true', and (c, '<', a) gives 'false' because the values are equal and '<' is strict.

Expected Output

Tables

variablesexpressions
variables
ColumnType
nameVARCHAR(10)
valueINTEGER
expressions
ColumnType
left_operandVARCHAR(10)
operatorVARCHAR(1)
right_operandVARCHAR(10)

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions