GREATEST and LEAST in MySQL - Time & Space Complexity
We want to understand how the time it takes to find the greatest or least value changes as we compare more values.
How does the number of values affect the work done by GREATEST and LEAST functions?
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 player.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing each value to find the greatest and least.
- How many times: Each value is compared once per function.
As the number of values to compare grows, the number of comparisons grows roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 5 | About 4 comparisons |
| 10 | About 9 comparisons |
| 100 | About 99 comparisons |
Pattern observation: The work grows steadily as you add more values to compare.
Time Complexity: O(n)
This means the time to find the greatest or least value grows in direct proportion to how many values you compare.
[X] Wrong: "GREATEST and LEAST instantly find the answer no matter how many values there are."
[OK] Correct: They must check each value to be sure which is biggest or smallest, so more values mean more work.
Understanding how simple functions like GREATEST and LEAST scale helps you explain efficiency clearly and shows you think about how queries perform as data grows.
"What if we used GREATEST on values from a subquery that returns many rows? How would that affect the time complexity?"
Practice
GREATEST(3, 7, 5) return?Solution
Step 1: Understand the GREATEST function
The GREATEST function returns the largest value among the given arguments.Step 2: Compare the values 3, 7, and 5
Among 3, 7, and 5, the largest number is 7.Final Answer:
7 -> Option DQuick Check:
GREATEST(3, 7, 5) = 7 [OK]
- Confusing GREATEST with LEAST
- Thinking it returns the first value
- Assuming it returns a sum
Solution
Step 1: Recall the function to find smallest value
In MySQL, the LEAST function returns the smallest value from a list of arguments.Step 2: Check the syntax options
LEAST(10, 20, 5) is the correct syntax. MIN is an aggregate function and requires a column, not a list of values. SMALLEST and LOWEST are not valid MySQL functions.Final Answer:
LEAST(10, 20, 5) -> Option BQuick Check:
LEAST(10, 20, 5) is correct syntax [OK]
- Using MIN() with values instead of columns
- Using non-existent functions like SMALLEST()
- Confusing function names
Scores with columns math, science, and english, what will the query SELECT GREATEST(math, science, english) AS top_score FROM Scores WHERE id = 1; return if the row with id = 1 has values (85, 90, 88)?Solution
Step 1: Identify the values for id = 1
The row has math=85, science=90, english=88.Step 2: Apply GREATEST to these values
GREATEST(85, 90, 88) returns the largest value, which is 90.Final Answer:
90 -> Option AQuick Check:
GREATEST(85, 90, 88) = 90 [OK]
- Selecting the first column value instead of greatest
- Confusing GREATEST with LEAST
- Assuming NULL if any value is less
SELECT LEAST(score1, score2, score3) FROM Results; It returns NULL for some rows even though all scores are numbers. What is the likely cause?Solution
Step 1: Understand LEAST behavior with NULLs
LEAST returns NULL if any argument is NULL because NULL means unknown value.Step 2: Check the cause of NULL results
If some rows have NULL in score1, score2, or score3, LEAST will return NULL for those rows.Final Answer:
One or more scores are NULL in those rows -> Option CQuick Check:
LEAST returns NULL if any input is NULL [OK]
- Assuming LEAST ignores NULLs
- Thinking LEAST only works with positive numbers
- Misreading LEAST as GREATEST
Students with columns exam1, exam2, and exam3. Which query correctly returns the student ID, highest score as max_score, and lowest score as min_score?Solution
Step 1: Understand GREATEST and LEAST usage
GREATEST returns the highest value among columns, LEAST returns the lowest.Step 2: Check the query syntax
SELECT student_id, GREATEST(exam1, exam2, exam3) AS max_score, LEAST(exam1, exam2, exam3) AS min_score FROM Students; correctly uses GREATEST and LEAST with column names and aliases them properly. MAX and MIN are aggregate functions and cannot take multiple columns as arguments directly.Final Answer:
Option A query -> Option AQuick Check:
Use GREATEST and LEAST for column-wise max/min [OK]
- Using MAX/MIN with multiple columns incorrectly
- Swapping GREATEST and LEAST aliases
- Syntax errors with aggregate functions
Ready to prove it?
Pass the test and this pattern moves unknown
