SQL MIN() And MAX() Functions

SQL Min And Max Functions

This is a detailed tutorial of SQL MIN() and MAX() functions. Learn to find the minimum and maximum values in database table records using these functions.

Use of SQL Min() and Max() Functions

SQL Min() and Max() functions are used to find the minimum and maximum values for a particular column in the database table records. This way, you can find out the smallest and largest value of a particular table column in a single query.

Min() and Max() Syntax

The syntax to use Min() and Max() function in any SQL query is very simple. Here below we’ve shown the syntax that makes use of this function in SQL SELECT Statement.

The resultset of this query is going to be only a single record and more precisely, just a single number. So, you can store the output in a column for which you can define your own name using AS keyword in SQL. This is also illustrated in the syntax given below. This will fetch the result in the resultset with the column name VAR_1.

Note. Again making use of a WHERE clause or any other clause is completely optional and depends upon your query logic.

Min() and Max() Function Examples

Here below, we’ve given one example for the MIN() and one for the MAX() function. For demonstration, we’re using a demo database table named marks which contains marks obtained by different students in different examinations.

The following screenshot gives you a quick look at the table structure and data.

SQL MIN() And MAX() Functions Demo Database

Finding the Minimum Value

The following query finds out the minimum value for the table column obtained_marks as Minimum_Marks.

SQL MIN() Function Example

You can see in the result set, there’s only one record labeled as Minimum_Marks and hence we found that the minimum value for the column obtained_marks is 0.

Finding the Maximum Value

The query written below will find out the maximum value of the obtained_marks column.

SQL MAX() Function Example

The resultset shows that the maximum value for the column obtained_marks is 92.

Finding Both The Maximum And The Minimum Value

The following query finds out the maximum as well as the minimum value of the column obtained_marks at the same time and it also does not make use of the AS keyword.

SQL MIN MAX Functions Example

You can see, now there are two columns in the result set with one record each and as we have not used the AS keyword here so the column names are the exactly queried column name with the function names itself as they are written in the query.

I hope you find this guide useful. If so, do share it with others who are willing to learn SQL. If you have any questions related to this article, feel free to ask them in the comments section.

Helpful Associated Guide.

Also, don’t forget to Subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *