Bird
Raised Fist0
PostgreSQLquery~10 mins

GREATEST and LEAST functions in PostgreSQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - GREATEST and LEAST functions
Input values
↓
Compare values pairwise
↓
Find Greatest value
→Return Greatest
↓
Find Least value
→Return Least
GREATEST and LEAST take multiple values, compare them, and return the largest or smallest value respectively.
Execution Sample
PostgreSQL
SELECT GREATEST(3, 7, 2) AS max_val, LEAST(3, 7, 2) AS min_val;
This query returns the largest and smallest values among 3, 7, and 2.
Execution Table
StepValues ComparedGREATEST ResultLEAST ResultExplanation
13, 773Compare first two values: 7 is greater, 3 is smaller
27, 272Compare previous greatest and next value: 7 still greatest, 2 smaller than previous least 3
3Final72Return final greatest and least values
4End72No more values to compare, query ends
💡 All values compared, final greatest is 7 and least is 2
Variable Tracker
VariableStartAfter 1After 2Final
GREATESTN/A777
LEASTN/A322
Key Moments - 2 Insights
Why does GREATEST return 7 and not 3 or 2?
Because in the execution_table rows 1 and 2, 7 is compared with other values and found to be the largest.
Why does LEAST return 2 instead of 3?
Row 2 shows LEAST compares 3 and 2, and 2 is smaller, so it updates the least value to 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the GREATEST result after step 1?
A7
B3
C2
DN/A
💡 Hint
Check the 'GREATEST Result' column in row 1 of execution_table
At which step does LEAST update from 3 to 2?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'LEAST Result' column in execution_table rows 1 and 2
If we add value 10 to the inputs, what would be the final GREATEST?
A7
B10
C3
D2
💡 Hint
GREATEST returns the largest value among all inputs
Concept Snapshot
GREATEST(value1, value2, ...) returns the largest value.
LEAST(value1, value2, ...) returns the smallest value.
They compare all inputs and return one value.
Useful for quick max/min among columns or expressions.
NULL inputs cause NULL result unless handled.
Full Transcript
The GREATEST and LEAST functions in PostgreSQL take multiple input values and compare them to find the largest and smallest values respectively. The process starts by comparing the first two values, updating the current greatest or least value. Then it compares the current greatest or least with the next value, updating if needed. This continues until all values are compared. The final greatest and least values are returned. For example, with inputs 3, 7, and 2, GREATEST returns 7 and LEAST returns 2. If a NULL is present, the result is NULL unless handled. This visual trace shows each comparison step and how the results update.

Practice

(1/5)
1. What does the GREATEST function do in PostgreSQL?
easy
A. Counts the number of values in a list
B. Returns the smallest value from a list of values
C. Returns the largest value from a list of values
D. Calculates the average of values in a list

Solution

  1. Step 1: Understand the function purpose

    The GREATEST function compares all given values and finds the largest one.
  2. Step 2: Compare with other options

    LEAST returns the smallest, counting and averaging are different functions.
  3. Final Answer:

    Returns the largest value from a list of values -> Option C
  4. Quick Check:

    GREATEST = largest value [OK]
Hint: GREATEST picks the biggest value from all inputs [OK]
Common Mistakes:
  • Confusing GREATEST with LEAST
  • Thinking it counts or averages values
  • Assuming it returns a boolean
2. Which of the following is the correct syntax to find the smallest value among 5, 10, and 3 using PostgreSQL?
easy
A. SELECT LEAST[5, 10, 3];
B. SELECT LEAST(5, 10, 3);
C. SELECT LEAST{5, 10, 3};
D. SELECT LEAST 5, 10, 3;

Solution

  1. Step 1: Recall correct function call syntax

    PostgreSQL functions use parentheses with comma-separated values.
  2. Step 2: Check each option

    Only SELECT LEAST(5, 10, 3); uses parentheses correctly; others use brackets, braces, or no parentheses.
  3. Final Answer:

    SELECT LEAST(5, 10, 3); -> Option B
  4. Quick Check:

    Function calls need parentheses [OK]
Hint: Use parentheses () for function arguments in SQL [OK]
Common Mistakes:
  • Using square brackets or braces instead of parentheses
  • Omitting parentheses around arguments
  • Separating arguments incorrectly
3. What will be the result of this query?
SELECT GREATEST(7, 2, 9, 4) AS max_val, LEAST(7, 2, 9, 4) AS min_val;
medium
A. max_val = 9, min_val = 2
B. max_val = 7, min_val = 4
C. max_val = 4, min_val = 2
D. max_val = 9, min_val = 4

Solution

  1. Step 1: Evaluate GREATEST function

    Among 7, 2, 9, 4, the largest value is 9.
  2. Step 2: Evaluate LEAST function

    Among the same values, the smallest is 2.
  3. Final Answer:

    max_val = 9, min_val = 2 -> Option A
  4. Quick Check:

    GREATEST=9, LEAST=2 [OK]
Hint: GREATEST picks max, LEAST picks min from values [OK]
Common Mistakes:
  • Mixing up max and min values
  • Choosing middle values instead of extremes
  • Ignoring all values except first or last
4. Identify the error in this query:
SELECT GREATEST(4, 8, 'ten') AS max_value;
medium
A. Missing closing parenthesis
B. No error, query runs fine
C. GREATEST function requires at least four arguments
D. Mixing integer and string types causes an error

Solution

  1. Step 1: Check argument types

    GREATEST compares values of the same type; mixing integers and strings causes a type error.
  2. Step 2: Verify syntax and argument count

    Parentheses are correct and function accepts any number of arguments greater than zero.
  3. Final Answer:

    Mixing integer and string types causes an error -> Option D
  4. Quick Check:

    Type mismatch causes error [OK]
Hint: All arguments must be same type for GREATEST/LEAST [OK]
Common Mistakes:
  • Assuming mixed types work without error
  • Thinking function needs fixed number of arguments
  • Ignoring type errors in SQL
5. You have a table scores with columns math, science, and english. Which query correctly finds the highest and lowest score for each student?
hard
A. SELECT student_id, GREATEST(math, science, english) AS highest, LEAST(math, science, english) AS lowest FROM scores;
B. SELECT student_id, MAX(math, science, english) AS highest, MIN(math, science, english) AS lowest FROM scores;
C. SELECT student_id, GREATEST(math, science, english) AS lowest, LEAST(math, science, english) AS highest FROM scores;
D. SELECT student_id, MAX(math) AS highest, MIN(english) AS lowest FROM scores;

Solution

  1. Step 1: Understand GREATEST and LEAST usage

    They compare multiple columns per row to find max and min values.
  2. 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.
  3. Final Answer:

    SELECT student_id, GREATEST(math, science, english) AS highest, LEAST(math, science, english) AS lowest FROM scores; -> Option A
  4. Quick Check:

    GREATEST/LEAST compare columns per row [OK]
Hint: Use GREATEST/LEAST to compare multiple columns in one row [OK]
Common Mistakes:
  • Using MAX/MIN with multiple columns incorrectly
  • Swapping highest and lowest functions
  • Comparing only one column instead of all
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