Challenge - 5 Problems
GREATEST and LEAST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of GREATEST with NULL values
What is the output of this PostgreSQL query?
SELECT GREATEST(5, NULL, 3);
PostgreSQL
SELECT GREATEST(5, NULL, 3);
Attempts:
2 left
💡 Hint
Remember how GREATEST handles NULL values in PostgreSQL.
✗ Incorrect
In PostgreSQL, GREATEST returns NULL if any argument is NULL unless you explicitly handle NULLs.
❓ query_result
intermediate2:00remaining
LEAST function with mixed data types
What is the result of this query?
Note: The values are strings.
SELECT LEAST('10', '2', '30');Note: The values are strings.
PostgreSQL
SELECT LEAST('10', '2', '30');
Attempts:
2 left
💡 Hint
Strings are compared lexicographically in PostgreSQL.
✗ Incorrect
LEAST compares strings lexicographically. '10' < '2' < '30' because the first characters '1' < '2' < '3', so '10' is the smallest.
📝 Syntax
advanced2:00remaining
Identify the syntax error in GREATEST usage
Which option contains a syntax error when using the GREATEST function?
Attempts:
2 left
💡 Hint
Check for trailing commas in function arguments.
✗ Incorrect
Option C has a trailing comma after 2, which is a syntax error in PostgreSQL function calls.
❓ optimization
advanced2:00remaining
Optimizing LEAST function with many columns
You want to find the smallest value among 10 numeric columns in a large table. Which approach is most efficient?
Attempts:
2 left
💡 Hint
Consider built-in function efficiency versus complex expressions.
✗ Incorrect
LEAST is optimized internally and faster than manual CASE or unpivoting for this use case.
🧠 Conceptual
expert3:00remaining
Behavior of GREATEST with mixed NULL and NOT NULL columns
Given a table with columns a, b, c where b can be NULL, what does this query return?
Assuming a=5, b=NULL, c=3 for id=1.
SELECT GREATEST(a, b, c) FROM table WHERE id = 1;
Assuming a=5, b=NULL, c=3 for id=1.
PostgreSQL
SELECT GREATEST(a, b, c) FROM table WHERE id = 1;
Attempts:
2 left
💡 Hint
How does GREATEST handle NULL values in columns?
✗ Incorrect
GREATEST returns NULL if any argument is NULL, so the presence of b=NULL causes the result to be NULL.