

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- 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 of an array in Arduino
- Get MAX and MIN values along with their row id in MySQL?
- Get the max n values from an array in JavaScript
- Get a random value between two values in MySQL?
- How to Get the Position of Max Value of a pandas Series?
- Get the last two values with MongoDB
- MySQL query to get the count of rows in which two or more specified values appear?
- How to find the minimum values of two or more fields in MySQL?
- MySQL always returning the bit values as blank? How to get the original values?
Advertisements