SQL MIN and MAX
SQL

SQL MIN and MAX — How to Find Minimum and Maximum Values

CHAPTER 13 of 30 SQL Tutorial — Free Course on Neotech Navigators
The SQL MIN() and MAX() functions return the smallest and largest values in a column. They work on numbers, text, and dates. In this chapter, you will learn how to find minimum and maximum values, combine them with WHERE, and use column aliases for cleaner output.

SQL MIN() Function

The MIN() function returns the smallest value in the selected column:

SQL — MIN Syntax
SELECT MIN(column_name) AS alias
FROM table_name;

We will use this Employees table:

TABLE: Employees
+----+-----------+------------+--------+-----------+
| ID | Name      | Department | Salary | City      |
+----+-----------+------------+--------+-----------+
| 1  | Priya K.  | Sales      | 55000  | Mumbai    |
| 2  | John D.   | Marketing  | 62000  | London    |
| 3  | Arun M.   | IT         | 78000  | Delhi     |
| 4  | Emma W.   | Sales      | 51000  | Sydney    |
| 5  | Ravi S.   | IT         | 72000  | Mumbai    |
| 6  | Sara L.   | HR         | 58000  | New York  |
+----+-----------+------------+--------+-----------+
SQL — Lowest Salary
SELECT MIN(Salary) AS LowestSalary
FROM Employees;
✓ OUTPUT
+--------------+
| LowestSalary |
+--------------+
| 51000        |
+--------------+

SQL MAX() Function

The MAX() function returns the largest value in the selected column:

SQL — Highest Salary
SELECT MAX(Salary) AS HighestSalary
FROM Employees;
✓ OUTPUT
+---------------+
| HighestSalary |
+---------------+
| 78000         |
+---------------+

MIN and MAX with WHERE

Combine with WHERE to find min/max within a filtered set:

SQL — Highest Salary in IT
SELECT MAX(Salary) AS TopITSalary
FROM Employees
WHERE Department = 'IT';
✓ OUTPUT
+-------------+
| TopITSalary |
+-------------+
| 78000       |
+-------------+
SQL — Lowest Salary in Sales
SELECT MIN(Salary) AS LowestSalesSalary
FROM Employees
WHERE Department = 'Sales';
✓ OUTPUT
+-------------------+
| LowestSalesSalary |
+-------------------+
| 51000             |
+-------------------+

MIN and MAX on Text Columns

MIN and MAX also work on text. MIN returns the alphabetically first value; MAX returns the last:

SQL
SELECT
  MIN(Name) AS FirstAlphabetically,
  MAX(Name) AS LastAlphabetically
FROM Employees;
✓ OUTPUT
+----------------------+---------------------+
| FirstAlphabetically  | LastAlphabetically  |
+----------------------+---------------------+
| Arun M.              | Sara L.             |
+----------------------+---------------------+

Using MIN and MAX Together

SQL — Salary Range
SELECT
  MIN(Salary) AS MinSalary,
  MAX(Salary) AS MaxSalary,
  MAX(Salary) - MIN(Salary) AS SalaryRange
FROM Employees;
✓ OUTPUT
+-----------+-----------+-------------+
| MinSalary | MaxSalary | SalaryRange |
+-----------+-----------+-------------+
| 51000     | 78000     | 27000       |
+-----------+-----------+-------------+

💡 Key Points

MIN and MAX are aggregate functions — they reduce multiple rows to a single result. They ignore NULL values automatically. Always use an alias (AS) so the output column has a meaningful name.

In the next chapter, you will learn the other essential aggregate functions: COUNT, AVG, and SUM. For a complete reference of aggregate functions, see the official MySQL aggregate functions documentation.

If you work with data regularly, explore our ready-made Dashboard Templates on NextGenTemplates that use SQL-powered data for business reporting.

📺 Visit our YouTube channel @NeoTechNavigators for step-by-step video tutorials and dashboard demos on this topic.

🧪 Try It Yourself

Write a query that shows the lowest and highest salary among employees in Mumbai.

ANSWER
SELECT
  MIN(Salary) AS LowestMumbai,
  MAX(Salary) AS HighestMumbai
FROM Employees
WHERE City = 'Mumbai';

Expected Output:

+--------------+---------------+
| LowestMumbai | HighestMumbai |
+--------------+---------------+
| 55000        | 72000         |
+--------------+---------------+

📝 What You Learned in This Chapter

  • MIN() returns the smallest value in a column
  • MAX() returns the largest value in a column
  • Both work on numbers, text, and dates
  • Combine with WHERE to find min/max in a filtered subset
  • Both functions ignore NULL values automatically
  • Always use AS to give the result column a meaningful name

Frequently Asked Questions

What is the difference between MIN and MAX in SQL?
MIN() returns the smallest value and MAX() returns the largest value in a column. For numbers, this means lowest and highest. For text, MIN returns the alphabetically first value and MAX returns the alphabetically last value.
Do MIN and MAX ignore NULL values?
Yes. Both functions automatically skip NULL values. If a column has values 10, NULL, 30, then MIN returns 10 and MAX returns 30. The NULL is ignored entirely.
Can I find which row has the MIN or MAX value?
MIN and MAX return only the value, not the entire row. To find the full row, use a subquery: SELECT * FROM Employees WHERE Salary = (SELECT MAX(Salary) FROM Employees). This returns all columns for the highest-paid employee.
Can I use MIN and MAX with GROUP BY?
Yes. GROUP BY combined with MIN/MAX lets you find the min or max per group. For example: SELECT Department, MAX(Salary) FROM Employees GROUP BY Department shows the highest salary in each department. GROUP BY is covered in Chapter 25.
Do MIN and MAX work on dates?
Yes. MIN(date_column) returns the earliest date and MAX(date_column) returns the latest date. This is useful for finding first and last order dates, earliest hire dates, and similar time-based queries.

📖 Chapter 13 of 30 — SQL Tutorial on Neotech Navigators

PK
Meet PK, the founder of NeotechNavigators.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your data analysis skills to the next level!
https://neotechnavigators.com