0
0
SQLquery~10 mins

MIN and MAX functions in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - MIN and MAX functions
Start with a column of values
Apply MIN function
Find smallest value
Apply MAX function
Find largest value
Return result as single value
MIN finds the smallest value in a column, MAX finds the largest, both return one value.
Execution Sample
SQL
SELECT MIN(age) AS Youngest, MAX(age) AS Oldest FROM people;
This query finds the youngest and oldest ages from the people table.
Execution Table
StepActionColumn ValuesMIN ResultMAX Result
1Read all ages[23, 45, 31, 19, 40]
2Find minimum age[23, 45, 31, 19, 40]19
3Find maximum age[23, 45, 31, 19, 40]1945
4Return results1945
💡 All values processed, MIN and MAX results returned
Variable Tracker
VariableStartAfter Step 2After Step 3Final
ages[23, 45, 31, 19, 40][23, 45, 31, 19, 40][23, 45, 31, 19, 40][23, 45, 31, 19, 40]
min_ageNULL191919
max_ageNULLNULL4545
Key Moments - 2 Insights
Why does MIN return only one value even though there are many ages?
MIN scans all values but returns only the smallest one as a single result, as shown in execution_table step 2.
Can MIN and MAX be used on text columns?
Yes, MIN returns the alphabetically first value and MAX the last, similar to numeric values but based on sorting order.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the MIN result after step 2?
A23
B45
C19
DNULL
💡 Hint
Check the 'MIN Result' column at step 2 in the execution_table.
At which step does the MAX function find the largest value?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'MAX Result' column in the execution_table to see when MAX is assigned.
If the ages were all the same, what would MIN and MAX return?
AThe same value
BNULL
CDifferent values
DError
💡 Hint
MIN and MAX return the smallest and largest values respectively; if all values are equal, both return that value.
Concept Snapshot
MIN(column) returns the smallest value in a column.
MAX(column) returns the largest value.
Both return a single value.
Used to find extremes in data.
Works on numbers and text (alphabetical order).
Full Transcript
The MIN and MAX functions in SQL help find the smallest and largest values in a column. For example, given ages 23, 45, 31, 19, and 40, MIN returns 19 and MAX returns 45. The process reads all values, finds the minimum and maximum, then returns these as single results. These functions work on numbers and text, returning the smallest or largest based on sorting. They always return one value, even if many rows exist.