Challenge - 5 Problems
Three-Valued Logic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of WHERE clause with NULL comparison
Consider a table
employees with a column manager_id that can be NULL. What rows will be returned by this query?SELECT * FROM employees WHERE manager_id = NULL;SQL
CREATE TABLE employees (id INT, name VARCHAR(50), manager_id INT); INSERT INTO employees VALUES (1, 'Alice', NULL), (2, 'Bob', 1), (3, 'Charlie', NULL);
Attempts:
2 left
💡 Hint
Remember that in SQL,
= NULL does not behave like normal equality.✗ Incorrect
In SQL, comparing any value to NULL with
= results in UNKNOWN, which is treated as FALSE in WHERE clauses. To check for NULL, use IS NULL.❓ query_result
intermediate2:00remaining
Effect of NOT on UNKNOWN in WHERE clause
Given a table
products with a nullable column discount, what rows will this query return?SELECT * FROM products WHERE NOT (discount > 10);SQL
CREATE TABLE products (id INT, name VARCHAR(50), discount INT); INSERT INTO products VALUES (1, 'Pen', 5), (2, 'Notebook', NULL), (3, 'Eraser', 15);
Attempts:
2 left
💡 Hint
Think about how NOT affects UNKNOWN in SQL's three-valued logic.
✗ Incorrect
In SQL,
discount > 10 is FALSE for <=10, UNKNOWN for NULL, TRUE for >10. NOT (discount > 10) is TRUE for <=10, UNKNOWN for NULL, FALSE for >10. WHERE clauses only return rows where the condition is TRUE, excluding UNKNOWN and FALSE.📝 Syntax
advanced2:00remaining
Identify the error in three-valued logic expression
Which of the following WHERE clauses will cause a syntax error due to incorrect use of three-valued logic operators?
Attempts:
2 left
💡 Hint
Check operator precedence and how NOT applies to IS NULL.
✗ Incorrect
Option C is invalid because
NOT status IS NULL is parsed as (NOT status) IS NULL, which is a syntax error. Correct usage is status IS NOT NULL.❓ optimization
advanced2:00remaining
Optimizing queries with three-valued logic and NULLs
You want to select rows where
score is either greater than 50 or unknown (NULL). Which query is more efficient and why?Attempts:
2 left
💡 Hint
Consider how COALESCE affects NULL handling and index usage.
✗ Incorrect
Option D uses COALESCE to replace NULL with 51, so the condition becomes a simple comparison, which can use indexes better and avoid OR conditions that are less efficient.
🧠 Conceptual
expert2:00remaining
Understanding three-valued logic truth tables
Given the SQL three-valued logic, what is the result of this expression?
TRUE AND UNKNOWN OR NOT FALSEAttempts:
2 left
💡 Hint
Evaluate step-by-step using SQL's three-valued logic truth tables.
✗ Incorrect
TRUE AND UNKNOWN is UNKNOWN; NOT FALSE is TRUE; UNKNOWN OR TRUE is TRUE.