
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Getting Minimum and Maximum Value in MySQL
We need to use the MAX(columnName) to find the Maximum value in a column, whereas use the MIN(columnName) to find the Maximum value in a column.
Let’s say following is the syntax to find the highest and lowest value in a specific column −
mysql> SELECT @min_val:=MIN(columnName),@max_val:=MAX(columnName) FROM tableName; mysql> SELECT * FROM tableName WHERE columnName=@min_val OR columnName=@max_val;
Note: Let’s say we have a database named ‘StudentsRecords’ and a table named ‘STUDENT.
Following is our table <STUDENT> −
StudentId | StudentMarks |
---|---|
S001 | 90 |
S002 | 97 |
S003 | 72 |
We will now write the query −
Query
mysql> SELECT @min_val:=MIN(StudentMarks),@max_val:=MAX(StudentMarks) FROM STUDENT; mysql> SELECT * FROM STUDENT WHERE StudentMarks =@min_val OR StudentMarks =@max_val;
Output
+---------------------+ | StudentMarks | +---------------------+ | 97 | +---------------------+
In the above query, ‘StudentMarks’refers to the name of the column. The ‘STUDENT’ refers to the name of the table from which the minimum and maximum value is being queried.
- Related Articles
- Getting the maximum value from a varchar field in MySQL
- Get maximum and minimum value in MongoDB?
- Path With Maximum Minimum Value in Python
- Finding the minimum and maximum value from a string with numbers separated by hyphen in MySQL?
- Get the minimum and maximum value from a VARCHAR column and display the result in separate MySQL columns?
- Getting last value in MySQL group concat?
- MySQL query to select maximum and minimum salary row?
- Getting maximum from a column value and set it for all other values in the same column with MySQL?
- Display the minimum and maximum value of primitive data types in Java
- Function that returns the minimum and maximum value of an array in JavaScript
- How to read Maximum and Minimum temperature in Six's Maximum and Minimum Thermometer?
- How can I get maximum and minimum values in a single MySQL query?
- How to find the minimum and maximum values in a single MySQL Query?
- Return maximum value from records in MySQL
- Find minimum unused value in a MySQL table?

Advertisements