0
0
PostgreSQLquery~5 mins

GREATEST and LEAST functions in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ANULL
B5
C3
D10
What does LEAST('apple', 'banana', 'cherry') return?
A'banana'
B'cherry'
C'apple'
DNULL
If one argument is NULL, what does GREATEST(4, NULL, 7) return?
ANULL
B4
CError
D7
Which function would you use to find the smallest value among columns?
AGREATEST
BLEAST
CMAX
DMIN
Can GREATEST be used to compare dates in PostgreSQL?
AYes, it returns the latest date.
BNo, it only works with numbers.
CYes, but only with strings.
DNo, it causes an error.
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.