Concept Flow - GREATEST and LEAST
Start with values
Compare values pairwise
GREATEST: Select largest value
LEAST: Select smallest value
Return result
GREATEST compares values and returns the largest; LEAST returns the smallest among given values.
SELECT GREATEST(3, 7, 5) AS max_val, LEAST(3, 7, 5) AS min_val;
| Step | Values Compared | GREATEST Result | LEAST Result | Explanation |
|---|---|---|---|---|
| 1 | 3 and 7 | 7 | 3 | Compare first two values: 7 is greater, 3 is smaller |
| 2 | 7 and 5 | 7 | 3 | Compare previous greatest 7 with 5: 7 remains greatest; least stays 3 |
| 3 | All values compared | 7 | 3 | Final greatest is 7, least is 3 |
| Variable | Start | After 1 | After 2 | Final |
|---|---|---|---|---|
| GREATEST | N/A | 7 | 7 | 7 |
| LEAST | N/A | 3 | 3 | 3 |
GREATEST(value1, value2, ...) returns the largest value. LEAST(value1, value2, ...) returns the smallest value. They compare all given values pairwise. Useful for quick max/min among multiple values. NULL input returns NULL if any value is NULL.
GREATEST(3, 7, 5) return?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)?SELECT LEAST(score1, score2, score3) FROM Results; It returns NULL for some rows even though all scores are numbers. What is the likely cause?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?Pass the test and this pattern moves unknown