SQL MAX() and MIN() Function

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

EmployeeIDFirstNameLastNameSalary
1JohnDoe60000
2JaneSmith75000
3MarkJohnson80000

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 

ProductIDProductNameCategoryPrice
1LaptopElectronics800
2SmartphoneElectronics500
3T-ShirtClothing20

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

UserIDFirstNameLastNameRegistrationDate
1JohnDoe2023-05-10
2JaneSmith2023-05-15
3MarkJohnson2023-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:
  1. SQL MAX() returns the maximum value, while SQL MIN() returns the minimum value. 
  2. MAX() is used to find the highest value, while MIN() is used to find the lowest value.
  3. 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:
  1. Ensure the column you’re applying MAX() or MIN() on contains the appropriate data type (numeric, date, or time).
  2. Use descriptive aliases for the result columns to enhance clarity in output tables.
  3. Combine MAX() or MIN() functions with other SQL clauses (e.g., WHERE, GROUP BY) to refine the results.
  4. Be cautious when using MAX() or MIN() on columns with NULL values, as they can affect the output.
SQL MAX() and MIN() Function

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.

SQL MAX() and MIN() Function

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.

SQL MAX() and MIN() Function

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?

SQL MAX() and MIN() Function

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.

SQL MAX() and MIN() Function

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription