Recall & Review
beginner
What does the GREATEST function do in SQL?
The GREATEST function returns the largest value from a list of expressions. It compares all values and gives back the highest one.
Click to reveal answer
beginner
What does the LEAST function do in SQL?
The LEAST function returns the smallest value from a list of expressions. It compares all values and gives back the lowest one.
Click to reveal answer
intermediate
How does GREATEST handle NULL values in PostgreSQL?
If any argument is NULL, GREATEST returns NULL unless you use COALESCE to replace NULLs with a value.
Click to reveal answer
beginner
Write a simple example using GREATEST to find the highest score among three columns: score1, score2, score3.
SELECT GREATEST(score1, score2, score3) AS highest_score FROM your_table;
Click to reveal answer
intermediate
Can GREATEST and LEAST be used with different data types like numbers and strings?
Yes, but all arguments must be comparable and usually of the same or compatible data types. For example, comparing strings works alphabetically.
Click to reveal answer
What will GREATEST(5, 10, 3) return?
✗ Incorrect
GREATEST returns the largest value, which is 10.
What does LEAST('apple', 'banana', 'cherry') return?
✗ Incorrect
LEAST returns the smallest value alphabetically, which is 'apple'.
If one argument is NULL, what does GREATEST(4, NULL, 7) return?
✗ Incorrect
GREATEST returns NULL if any argument is NULL unless handled with COALESCE.
Which function would you use to find the smallest value among columns?
✗ Incorrect
LEAST returns the smallest value among the given expressions.
Can GREATEST be used to compare dates in PostgreSQL?
✗ Incorrect
GREATEST can compare dates and returns the latest (greatest) date.
Explain how the GREATEST and LEAST functions work and give a simple example for each.
Think about comparing values to find the highest or lowest.
You got /4 concepts.
Describe how NULL values affect the output of GREATEST and LEAST in PostgreSQL and how to handle them.
Consider what happens when you compare something unknown (NULL).
You got /3 concepts.