MySQL MIN and MAX
The MIN
and MAX
functions return the smallest and largest values in a specified column, respectively. They are commonly used for numerical and date-based data.
Examples with Tamil Kings
1. Using MIN to Find the Earliest Reign
SELECT MIN(reign_period) AS earliest_reign FROM tamil_kings_auto_increment;
Code Explanation: This query returns the earliest reign_period
from the tamil_kings_auto_increment
table.
2. Using MAX to Find the Latest Reign
SELECT MAX(reign_period) AS latest_reign FROM tamil_kings_auto_increment;
Code Explanation: This query returns the latest reign_period
from the tamil_kings_auto_increment
table.
Best Practices
- Use
MIN
andMAX
to identify the range of values in a dataset. - Ensure the data type of the column is appropriate for the comparison (e.g., numeric or date).
Key Takeaways
MIN
returns the smallest value in a column.MAX
returns the largest value in a column.