SQL MAX() and MIN() Function

Introduction
SQL MAX() and MIN() function are two essential aggregate functions used in SQL queries. These functions allow you to find the maximum and minimum values from a specific column in a table. In this article, we will explore the usage, syntax, and examples of both functions.
SQL MAX() Function :
The SQL MAX() function is used to retrieve the maximum value from a specified column in a table. It is commonly used to find the highest value within a dataset. Here is the syntax for using the MAX() function:
Syntax :
SELECT MAX(column_name) FROM table_name;
- To better understand how the MAX() function works, let’s look at a few examples:
Example 1: Finding the Maximum Salary
Consider a table called “Employees” with the following columns: “EmployeeID,” “FirstName,” “LastName,” and “Salary.” We can use the MAX() function to find the employee with the highest salary.
Table: Employees
EmployeeID | FirstName | LastName | Salary |
---|---|---|---|
1 | John | Doe | 60000 |
2 | Jane | Smith | 75000 |
3 | Mark | Johnson | 80000 |
Syntax :
SELECT MAX(Salary) AS MaxSalary FROM Employees;
Output Table :
MaxSalary |
---|
80000 |
SQL MIN() Function :
The SQL MIN() function, on the other hand, is used to retrieve the minimum value from a specified column in a table. It is often used to find the lowest value within a dataset. Here is the syntax for using the MIN() function:
Syntax :
SELECT MIN(column_name) FROM table_name;
- Let’s explore a couple of examples to grasp the concept of the MIN() function:
Example 2: Finding the Minimum Price
Consider a table called “Products” with columns such as “ProductID,” “ProductName,” “Category,” and “Price.” We can use the MIN() function to find the product with the lowest price.
Table: Products
ProductID | ProductName | Category | Price |
---|---|---|---|
1 | Laptop | Electronics | 800 |
2 | Smartphone | Electronics | 500 |
3 | T-Shirt | Clothing | 20 |
Syntax :
SELECT MIN(Price) AS MinPrice FROM Products;
Output Table :
MinPrice |
---|
20 |
Example 3: Finding the Earliest Registration Date
Let’s say we have a table called “Users” with columns like “UserID,” “FirstName,” “LastName,” and “RegistrationDate.” To retrieve the earliest registration date, we can use the MIN() function.
Table: Users
UserID | FirstName | LastName | RegistrationDate |
---|---|---|---|
1 | John | Doe | 2023-05-10 |
2 | Jane | Smith | 2023-05-15 |
3 | Mark | Johnson | 2023-05-12 |
Syntax :
SELECT MIN(RegistrationDate) AS EarliestRegistrationDate FROM Users;
Output Table :EarliestRegistrationDate
EarliestRegistrationDate |
---|
2023-05-10 |
- While both SQL MAX() and MIN() functions are used to retrieve values from a specific column, they have distinct differences:
- SQL MAX() returns the maximum value, while SQL MIN() returns the minimum value.
- MAX() is used to find the highest value, while MIN() is used to find the lowest value.
- MAX() and MIN() can be applied to numerical as well as date and time columns.
- When using SQL MAX() and MIN() functions, consider the following best practices:
- Ensure the column you’re applying MAX() or MIN() on contains the appropriate data type (numeric, date, or time).
- Use descriptive aliases for the result columns to enhance clarity in output tables.
- Combine MAX() or MIN() functions with other SQL clauses (e.g., WHERE, GROUP BY) to refine the results.
- Be cautious when using MAX() or MIN() on columns with NULL values, as they can affect the output.

Question 1. What is the purpose of the MAX() function in SQL?
The MAX() function is used to retrieve the maximum value from a specified column in a table.

Question 2. How does the MIN() function differ from the MAX() function in SQL?
The MIN() function retrieves the minimum value, whereas the MAX() function retrieves the maximum value from a column.

Question 3. Can I use the MAX() and MIN() functions on date columns?
How does the MIN() function differ from the MAX() function in SQL?

Question 4. What should I do if the column I apply MAX() or MIN() on contains NULL values?
Be cautious when dealing with NULL values, as they can affect the results. Consider using additional SQL clauses like WHERE to exclude NULL values if necessary.

Question 5. Are there any best practices for using SQL MAX() and MIN() functions?
Yes, some best practices include using appropriate column types, providing descriptive aliases, combining with other SQL clauses, and handling NULL values carefully.
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment