Bird
Raised Fist0
MySQLquery~20 mins

GREATEST and LEAST in MySQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
GREATEST and LEAST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
1:30remaining
Output of GREATEST with NULL values
What is the output of the following MySQL query?

SELECT GREATEST(5, NULL, 3) AS result;
MySQL
SELECT GREATEST(5, NULL, 3) AS result;
A3
B5
CNULL
D0
Attempts:
2 left
💡 Hint
Remember how GREATEST handles NULL values in MySQL.
❓ query_result
intermediate
1:30remaining
LEAST function with negative and positive numbers
What is the output of this query?

SELECT LEAST(-10, 0, 5, 3) AS result;
MySQL
SELECT LEAST(-10, 0, 5, 3) AS result;
A-10
B0
C3
D5
Attempts:
2 left
💡 Hint
LEAST returns the smallest value among the arguments.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in GREATEST usage
Which option contains a syntax error when using the GREATEST function in MySQL?
ASELECT GREATEST(1, 2, 3) AS max_value;
BSELECT GREATEST(4, 5, ) AS max_value;
CSELECT GREATEST(2, 3, NULL) AS max_value;
DSELECT GREATEST(5, '10', 7) AS max_value;
Attempts:
2 left
💡 Hint
Look for misplaced commas or missing arguments.
❓ optimization
advanced
2:00remaining
Optimizing LEAST with columns and constants
Given a table scores with columns math, science, and english, which query efficiently returns the lowest score for each student?
ASELECT LEAST(math, science) + LEAST(english, 0) AS lowest_score FROM scores;
BSELECT CASE WHEN math < science AND math < english THEN math WHEN science < english THEN science ELSE english END AS lowest_score FROM scores;
CSELECT MIN(math, science, english) AS lowest_score FROM scores;
DSELECT LEAST(math, science, english) AS lowest_score FROM scores;
Attempts:
2 left
💡 Hint
Consider built-in functions that compare multiple values in one call.
🧠 Conceptual
expert
2:30remaining
Behavior of GREATEST and LEAST with different data types
What is the result of this query?

SELECT GREATEST('10', 5, '7') AS greatest_val, LEAST('10', 5, '7') AS least_val;

Assume MySQL default type conversion rules.
MySQL
SELECT GREATEST('10', 5, '7') AS greatest_val, LEAST('10', 5, '7') AS least_val;
Agreatest_val = 10, least_val = 5
Bgreatest_val = 10, least_val = 5 (both as strings)
Cgreatest_val = 10, least_val = 7
Dgreatest_val = '10', least_val = '7'
Attempts:
2 left
💡 Hint
MySQL converts strings to numbers when comparing numeric strings and numbers.

Practice

(1/5)
1. What does the MySQL function GREATEST(3, 7, 5) return?
easy
A. NULL
B. 3
C. 5
D. 7

Solution

  1. Step 1: Understand the GREATEST function

    The GREATEST function returns the largest value among the given arguments.
  2. Step 2: Compare the values 3, 7, and 5

    Among 3, 7, and 5, the largest number is 7.
  3. Final Answer:

    7 -> Option D
  4. Quick Check:

    GREATEST(3, 7, 5) = 7 [OK]
Hint: GREATEST picks the highest number from the list [OK]
Common Mistakes:
  • Confusing GREATEST with LEAST
  • Thinking it returns the first value
  • Assuming it returns a sum
2. Which of the following is the correct syntax to find the smallest value among 10, 20, and 5 using MySQL?
easy
A. MIN(10, 20, 5)
B. LEAST(10, 20, 5)
C. SMALLEST(10, 20, 5)
D. LOWEST(10, 20, 5)

Solution

  1. Step 1: Recall the function to find smallest value

    In MySQL, the LEAST function returns the smallest value from a list of arguments.
  2. 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.
  3. Final Answer:

    LEAST(10, 20, 5) -> Option B
  4. Quick Check:

    LEAST(10, 20, 5) is correct syntax [OK]
Hint: Use LEAST() for smallest, GREATEST() for largest values [OK]
Common Mistakes:
  • Using MIN() with values instead of columns
  • Using non-existent functions like SMALLEST()
  • Confusing function names
3. Given the table 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)?
medium
A. 90
B. 85
C. 88
D. NULL

Solution

  1. Step 1: Identify the values for id = 1

    The row has math=85, science=90, english=88.
  2. Step 2: Apply GREATEST to these values

    GREATEST(85, 90, 88) returns the largest value, which is 90.
  3. Final Answer:

    90 -> Option A
  4. Quick Check:

    GREATEST(85, 90, 88) = 90 [OK]
Hint: GREATEST returns the highest among given column values [OK]
Common Mistakes:
  • Selecting the first column value instead of greatest
  • Confusing GREATEST with LEAST
  • Assuming NULL if any value is less
4. Consider this query: SELECT LEAST(score1, score2, score3) FROM Results; It returns NULL for some rows even though all scores are numbers. What is the likely cause?
medium
A. Syntax error in the query
B. LEAST function only works with positive numbers
C. One or more scores are NULL in those rows
D. LEAST returns the largest value, so NULL is unexpected

Solution

  1. Step 1: Understand LEAST behavior with NULLs

    LEAST returns NULL if any argument is NULL because NULL means unknown value.
  2. 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.
  3. Final Answer:

    One or more scores are NULL in those rows -> Option C
  4. Quick Check:

    LEAST returns NULL if any input is NULL [OK]
Hint: Check for NULLs in inputs; LEAST returns NULL if any argument is NULL [OK]
Common Mistakes:
  • Assuming LEAST ignores NULLs
  • Thinking LEAST only works with positive numbers
  • Misreading LEAST as GREATEST
5. You want to find the highest and lowest scores among three exams for each student in a table 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?
hard
A. SELECT student_id, GREATEST(exam1, exam2, exam3) AS max_score, LEAST(exam1, exam2, exam3) AS min_score FROM Students;
B. SELECT student_id, MAX(exam1, exam2, exam3) AS max_score, MIN(exam1, exam2, exam3) AS min_score FROM Students;
C. SELECT student_id, GREATEST(exam1, exam2, exam3) AS min_score, LEAST(exam1, exam2, exam3) AS max_score FROM Students;
D. SELECT student_id, MAX(exam1, exam2, exam3) AS min_score, MIN(exam1, exam2, exam3) AS max_score FROM Students;

Solution

  1. Step 1: Understand GREATEST and LEAST usage

    GREATEST returns the highest value among columns, LEAST returns the lowest.
  2. 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.
  3. Final Answer:

    Option A query -> Option A
  4. Quick Check:

    Use GREATEST and LEAST for column-wise max/min [OK]
Hint: Use GREATEST/LEAST for row-wise max/min, not MAX/MIN [OK]
Common Mistakes:
  • Using MAX/MIN with multiple columns incorrectly
  • Swapping GREATEST and LEAST aliases
  • Syntax errors with aggregate functions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard