0
0
PostgreSQLquery~20 mins

GREATEST and LEAST functions in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GREATEST and LEAST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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);
AError
B5
C3
DNULL
Attempts:
2 left
💡 Hint
Remember how GREATEST handles NULL values in PostgreSQL.
query_result
intermediate
2:00remaining
LEAST function with mixed data types
What is the result of this query?

SELECT LEAST('10', '2', '30');

Note: The values are strings.
PostgreSQL
SELECT LEAST('10', '2', '30');
A'10'
BError
C'30'
D'2'
Attempts:
2 left
💡 Hint
Strings are compared lexicographically in PostgreSQL.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in GREATEST usage
Which option contains a syntax error when using the GREATEST function?
ASELECT GREATEST(1, 2, 3);
BSELECT GREATEST(5, 10);
CSELECT GREATEST(1, 2,);
DSELECT GREATEST(7);
Attempts:
2 left
💡 Hint
Check for trailing commas in function arguments.
optimization
advanced
2: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?
AUse LEAST(col1, col2, ..., col10) in SELECT clause directly.
BUse a CASE expression comparing each column pairwise.
CUnpivot columns into rows and use MIN() aggregation.
DCreate a user-defined function to loop through columns.
Attempts:
2 left
💡 Hint
Consider built-in function efficiency versus complex expressions.
🧠 Conceptual
expert
3: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?

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;
A3
BNULL
C5
DError
Attempts:
2 left
💡 Hint
How does GREATEST handle NULL values in columns?