MIN and MAX
Find the smallest and largest values in your data.
- MIN â the minimum value in a set
- MAX â the maximum value in a set Every dataset has boundaries. These functions find them.
MIN Function
-- Smallest salary in the company
SELECT MIN(salary) AS lowest_salary
FROM employees;
-- Earliest hire date
SELECT MIN(hire_date) AS first_hire
FROM employees;
-- Alphabetically first name
SELECT MIN(first_name) AS first_name_alphabetically
FROM employees;
MAX Function
-- Highest salary in the company
SELECT MAX(salary) AS highest_salary
FROM employees;
-- Most recent hire date
SELECT MAX(hire_date) AS latest_hire
FROM employees;
-- Last name alphabetically
SELECT MAX(last_name) AS last_name_alphabetically
FROM employees;
MIN and MAX with GROUP BY
-- Salary range per department
SELECT department,
MIN(salary) AS min_salary,
MAX(salary) AS max_salary,
MAX(salary) - MIN(salary) AS salary_range
FROM employees
GROUP BY department
ORDER BY salary_range DESC;
-- Earliest and latest hire dates per department
SELECT department,
MIN(hire_date) AS first_hire,
MAX(hire_date) AS last_hire
FROM employees
GROUP BY department;
Finding the Row with MAX Value
-- Get the employee with the highest salary (single row)
SELECT *
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);
-- Get top N highest salaries
SELECT *
FROM employees
ORDER BY salary DESC
LIMIT 5;
-- Get employees with maximum salary per department
SELECT e.*
FROM employees e
WHERE salary = (
SELECT MAX(salary)
FROM employees
WHERE department = e.department
);
MIN and MAX with Dates
-- Date range analysis
SELECT MIN(order_date) AS first_order,
MAX(order_date) AS last_order,
DATEDIFF(DAY, MIN(order_date), MAX(order_date)) AS days_span
FROM orders;
-- Orders per month with date boundaries
SELECT YEAR(order_date) AS order_year,
MONTH(order_date) AS order_month,
MIN(order_date) AS month_start,
MAX(order_date) AS month_end,
COUNT(*) AS order_count
FROM orders
GROUP BY YEAR(order_date), MONTH(order_date)
ORDER BY order_year, order_month;
MIN and MAX with Strings
-- String ordering uses lexicographic comparison
SELECT MIN(product_name) AS first_product,
MAX(product_name) AS last_product
FROM products;
-- Find products alphabetically within each category
SELECT category,
MIN(product_name) AS first_product,
MAX(product_name) AS last_product
FROM products
GROUP BY category;
MIN and MAX with CASE
-- Conditional MIN/MAX
SELECT department,
MIN(CASE WHEN gender = 'M' THEN salary END) AS min_male_salary,
MAX(CASE WHEN gender = 'M' THEN salary END) AS max_male_salary,
MIN(CASE WHEN gender = 'F' THEN salary END) AS min_female_salary,
MAX(CASE WHEN gender = 'F' THEN salary END) AS max_female_salary
FROM employees
GROUP BY department;
Window Functions with MIN/MAX
-- Compare each employee's salary to department min/max
SELECT employee_name, department, salary,
MIN(salary) OVER (PARTITION BY department) AS dept_min,
MAX(salary) OVER (PARTITION BY department) AS dept_max,
ROUND((salary - MIN(salary) OVER (PARTITION BY department)) * 100.0 /
NULLIF(MAX(salary) OVER (PARTITION BY department) - MIN(salary) OVER (PARTITION BY department), 0), 1) AS pct_in_range
FROM employees;
-- Running min and max
SELECT order_date, amount,
MIN(amount) OVER (ORDER BY order_date) AS running_min,
MAX(amount) OVER (ORDER BY order_date) AS running_max
FROM orders;
Common MIN/MAX Patterns
-- Find duplicate values (min = max in a group)
SELECT department, job_title, COUNT(*) AS cnt
FROM employees
GROUP BY department, job_title
HAVING MIN(salary) = MAX(salary);
-- Detect outliers (values outside 2x the average)
SELECT *
FROM employees
WHERE salary > 2 * (SELECT AVG(salary) FROM employees);
-- Find the most recent record per group
SELECT e.*
FROM employees e
WHERE hire_date = (
SELECT MAX(hire_date)
FROM employees
WHERE department = e.department
);
NULL Handling
-- MIN and MAX ignore NULLs
SELECT MIN(salary) AS min_salary, -- Ignores NULLs
MAX(salary) AS max_salary -- Ignores NULLs
FROM employees;
-- If all values are NULL, result is NULL
SELECT MIN(NULL) AS result; -- Returns NULL
Performance Tips
| Operation | Method |
|---|---|
| Global min/max | SELECT MIN/MAX directly |
| Per-group min/max | Use GROUP BY |
| Row with max value | Subquery or ORDER BY LIMIT |
| Range calculation | MAX - MIN |
| Percentile position | Window functions |
| Outlier detection | Compare to MIN/MAX or AVG |
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type â Always start by categorizing the problem
- Incorrect formula application â Verify the formula applies to your specific case
- Unit errors â Always check units are consistent throughout
- Sign errors â Pay careful attention to positive and negative values
- Rounding too early â Keep full precision until the final answer
- Forgetting constraints â Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic â Build on the foundation laid here
- Next Topic â See how this concept extends to more advanced material
- Practice Problems â Additional exercises with solutions
- Video Explanation â Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions