- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the max of two values MySQL?
To get the maximum of two values in MySQL, we can use the predefined function “greatest”. The syntax of greatest() function is as follows −
SELECT greatest(value1,value2);
Applying the above query, To get the maximum value from two values. The query is as follows −
Case 1
We are giving both the values int.
mysql> SELECT greatest(100,-300);
After executing the above query, we will get the following output
+--------------------+ | greatest(100,-300) | +--------------------+ | 100 | +--------------------+ 1 row in set (0.00 sec)
Case 2
We are giving both the values string −
The query is as follows −
mysql> SELECT greatest('A','a');
The following is the output −
+-------------------+ | greatest('A','a') | +-------------------+ | a | +-------------------+ 1 row in set (0.00 sec)
Here you can see ‘a’ is displayed. This is because the ASCII value of ‘a’ is 97 and ‘A’ is 65. Therefore, the value 97 is greater than 65, that means ’a’ is greater than ‘A’.
Let us check now. The query for ‘a’ −
mysql> SELECT ascii('a');
Here is the output
+------------+ | ascii('a') | +------------+ | 97 | +------------+ 1 row in set (0.00 sec)
The query for ‘A’.
mysql> SELECT ascii('A');
The following is the output
+------------+ | ascii('A') | +------------+ | 65 | +------------+ 1 row in set (0.00 sec)
- Related Articles
- How to get the greatest of two columns values in MySQL?
- Get MAX() on column in two MySQL tables?
- MySQL query to get the max value with numeric values in varchar field?
- How to get max(id) of row data in MySQL?
- MySQL query to get max id from varchar type and the values in numeric?
- How to get max values for distinct elements in MongoDB
- Get MAX and MIN values along with their row id in MySQL?
- Get max and min values of an array in Arduino
- How to select multiple max values which would be duplicate values as well in MYSQL?
- Get the max n values from an array in JavaScript
- Get a random value between two values in MySQL?
- MySQL always returning the bit values as blank? How to get the original values?
- How to Get the Position of Max Value of a pandas Series?
- MySQL query to get first two highest column values from a table?
- MySQL Query to get count of unique values?

Advertisements