GREATEST and LEAST functions in PostgreSQL - Time & Space Complexity
We want to understand how the time it takes to find the greatest or least value changes as we give more values to compare.
How does the work grow when using GREATEST or LEAST with more inputs?
Analyze the time complexity of the following code snippet.
SELECT GREATEST(score1, score2, score3, score4, score5) AS max_score,
LEAST(score1, score2, score3, score4, score5) AS min_score
FROM player_scores;
This code finds the highest and lowest scores among five columns for each row in the table.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing each value to find the greatest and least.
- How many times: Each row compares all given values once.
As you add more values to compare, the number of comparisons grows roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 5 | 4 comparisons |
| 10 | 9 comparisons |
| 100 | 99 comparisons |
Pattern observation: The number of comparisons increases directly with the number of values.
Time Complexity: O(n)
This means the time to find the greatest or least value grows linearly as you add more values to compare.
[X] Wrong: "GREATEST and LEAST instantly find the answer no matter how many values there are."
[OK] Correct: The functions must check each value to be sure which is largest or smallest, so more values mean more work.
Understanding how these functions scale helps you explain performance when working with many columns or values, showing you know how database operations grow with input size.
"What if we used GREATEST and LEAST on values stored in an array instead of separate columns? How would the time complexity change?"
Practice
GREATEST function do in PostgreSQL?Solution
Step 1: Understand the function purpose
TheGREATESTfunction compares all given values and finds the largest one.Step 2: Compare with other options
LEASTreturns the smallest, counting and averaging are different functions.Final Answer:
Returns the largest value from a list of values -> Option CQuick Check:
GREATEST = largest value [OK]
- Confusing GREATEST with LEAST
- Thinking it counts or averages values
- Assuming it returns a boolean
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 BQuick Check:
Function calls need parentheses [OK]
- Using square brackets or braces instead of parentheses
- Omitting parentheses around arguments
- Separating arguments incorrectly
SELECT GREATEST(7, 2, 9, 4) AS max_val, LEAST(7, 2, 9, 4) AS min_val;
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 AQuick Check:
GREATEST=9, LEAST=2 [OK]
- Mixing up max and min values
- Choosing middle values instead of extremes
- Ignoring all values except first or last
SELECT GREATEST(4, 8, 'ten') AS max_value;
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 DQuick Check:
Type mismatch causes error [OK]
- Assuming mixed types work without error
- Thinking function needs fixed number of arguments
- Ignoring type errors in SQL
scores with columns math, science, and english. Which query correctly finds the highest and lowest score for each student?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 AQuick Check:
GREATEST/LEAST compare columns per row [OK]
- Using MAX/MIN with multiple columns incorrectly
- Swapping highest and lowest functions
- Comparing only one column instead of all
Ready to prove it?
Pass the test and this pattern moves unknown
