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
✗ Incorrect
GREATEST returns the largest value, which is 10.
What does LEAST('apple', 'banana', 'cherry') return?
A'banana'
B'cherry'
C'apple'
DNULL
✗ Incorrect
LEAST returns the smallest value alphabetically, which is 'apple'.
If one argument is NULL, what does GREATEST(4, NULL, 7) return?
ANULL
B4
CError
D7
✗ 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?
AGREATEST
BLEAST
CMAX
DMIN
✗ Incorrect
LEAST returns the smallest value among the given expressions.
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.
✗ 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.
Practice
(1/5)
1. What does the GREATEST function do in PostgreSQL?
easy
A. Counts the number of values in a list
B. Returns the smallest value from a list of values
C. Returns the largest value from a list of values
D. Calculates the average of values in a list
Solution
Step 1: Understand the function purpose
The GREATEST function compares all given values and finds the largest one.
Step 2: Compare with other options
LEAST returns the smallest, counting and averaging are different functions.
Final Answer:
Returns the largest value from a list of values -> Option C
Quick Check:
GREATEST = largest value [OK]
Hint: GREATEST picks the biggest value from all inputs [OK]
Common Mistakes:
Confusing GREATEST with LEAST
Thinking it counts or averages values
Assuming it returns a boolean
2. Which of the following is the correct syntax to find the smallest value among 5, 10, and 3 using PostgreSQL?
easy
A. SELECT LEAST[5, 10, 3];
B. SELECT LEAST(5, 10, 3);
C. SELECT LEAST{5, 10, 3};
D. SELECT LEAST 5, 10, 3;
Solution
Step 1: Recall correct function call syntax
PostgreSQL functions use parentheses with comma-separated values.
Step 2: Check each option
Only SELECT LEAST(5, 10, 3); uses parentheses correctly; others use brackets, braces, or no parentheses.
Final Answer:
SELECT LEAST(5, 10, 3); -> Option B
Quick Check:
Function calls need parentheses [OK]
Hint: Use parentheses () for function arguments in SQL [OK]
Common Mistakes:
Using square brackets or braces instead of parentheses
Omitting parentheses around arguments
Separating arguments incorrectly
3. What will be the result of this query?
SELECT GREATEST(7, 2, 9, 4) AS max_val, LEAST(7, 2, 9, 4) AS min_val;
medium
A. max_val = 9, min_val = 2
B. max_val = 7, min_val = 4
C. max_val = 4, min_val = 2
D. max_val = 9, min_val = 4
Solution
Step 1: Evaluate GREATEST function
Among 7, 2, 9, 4, the largest value is 9.
Step 2: Evaluate LEAST function
Among the same values, the smallest is 2.
Final Answer:
max_val = 9, min_val = 2 -> Option A
Quick Check:
GREATEST=9, LEAST=2 [OK]
Hint: GREATEST picks max, LEAST picks min from values [OK]
Common Mistakes:
Mixing up max and min values
Choosing middle values instead of extremes
Ignoring all values except first or last
4. Identify the error in this query:
SELECT GREATEST(4, 8, 'ten') AS max_value;
medium
A. Missing closing parenthesis
B. No error, query runs fine
C. GREATEST function requires at least four arguments
D. Mixing integer and string types causes an error
Solution
Step 1: Check argument types
GREATEST compares values of the same type; mixing integers and strings causes a type error.
Step 2: Verify syntax and argument count
Parentheses are correct and function accepts any number of arguments greater than zero.
Final Answer:
Mixing integer and string types causes an error -> Option D
Quick Check:
Type mismatch causes error [OK]
Hint: All arguments must be same type for GREATEST/LEAST [OK]
Common Mistakes:
Assuming mixed types work without error
Thinking function needs fixed number of arguments
Ignoring type errors in SQL
5. You have a table scores with columns math, science, and english. Which query correctly finds the highest and lowest score for each student?
hard
A. SELECT student_id, GREATEST(math, science, english) AS highest, LEAST(math, science, english) AS lowest FROM scores;
B. SELECT student_id, MAX(math, science, english) AS highest, MIN(math, science, english) AS lowest FROM scores;
C. SELECT student_id, GREATEST(math, science, english) AS lowest, LEAST(math, science, english) AS highest FROM scores;
D. SELECT student_id, MAX(math) AS highest, MIN(english) AS lowest FROM scores;
Solution
Step 1: Understand GREATEST and LEAST usage
They compare multiple columns per row to find max and min values.
Step 2: Analyze each option
SELECT student_id, GREATEST(math, science, english) AS highest, LEAST(math, science, english) AS lowest FROM scores; correctly uses GREATEST and LEAST on columns per student. SELECT student_id, MAX(math, science, english) AS highest, MIN(math, science, english) AS lowest FROM scores; uses MAX/MIN incorrectly with multiple columns. SELECT student_id, GREATEST(math, science, english) AS lowest, LEAST(math, science, english) AS highest FROM scores; swaps highest and lowest. SELECT student_id, MAX(math) AS highest, MIN(english) AS lowest FROM scores; only compares single columns, not all three.
Final Answer:
SELECT student_id, GREATEST(math, science, english) AS highest, LEAST(math, science, english) AS lowest FROM scores; -> Option A
Quick Check:
GREATEST/LEAST compare columns per row [OK]
Hint: Use GREATEST/LEAST to compare multiple columns in one row [OK]
Common Mistakes:
Using MAX/MIN with multiple columns incorrectly
Swapping highest and lowest functions
Comparing only one column instead of all
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.