Introduction
GREATEST and LEAST help you find the biggest or smallest value from a list of values easily.
GREATEST(value1, value2, ..., valueN) LEAST(value1, value2, ..., valueN)
SELECT GREATEST(10, 20, 30);
SELECT LEAST(10, 20, 30);
SELECT GREATEST(price, discount, 100) FROM products;
SELECT LEAST(score1, score2, score3) FROM exams;
CREATE TABLE test_scores ( student VARCHAR(20), math INT, science INT, english INT ); INSERT INTO test_scores VALUES ('Alice', 85, 90, 78), ('Bob', 70, 65, 80), ('Charlie', 95, 88, 92); SELECT student, GREATEST(math, science, english) AS highest_score, LEAST(math, science, english) AS lowest_score FROM test_scores;
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