
- 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
Get MySQL maximum value from 3 different columns?
To get the maximum value from three different columns, use the GREATEST() function.
The syntax is as follows
SELECT GREATEST(yourColumnName1,yourColumnName2,yourColumnName3) AS anyAliasName FROM yourTableName;
To understand the above syntax, let us create a table. The query to create a table is as follows
mysql> create table MaxOfThreeColumnsDemo -> ( -> First int, -> Second int, -> Third int -> ); Query OK, 0 rows affected (0.73 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into MaxOfThreeColumnsDemo values(30,90,60); Query OK, 1 row affected (0.16 sec) mysql> insert into MaxOfThreeColumnsDemo values(100,40,50); Query OK, 1 row affected (0.20 sec) mysql> insert into MaxOfThreeColumnsDemo values(101,290,150); Query OK, 1 row affected (0.22 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from MaxOfThreeColumnsDemo;
The following is the output
+-------+--------+-------+ | First | Second | Third | +-------+--------+-------+ | 30 | 90 | 60 | | 100 | 40 | 50 | | 101 | 290 | 150 | +-------+--------+-------+ 3 rows in set (0.00 sec)
Here is the query to find the greatest of three columns
mysql> select greatest(First,Second,Third) AS MAXValueOfThreeColumns from MaxOfThreeColumnsDemo;
The following is the output
+------------------------+ | MAXValueOfThreeColumns | +------------------------+ | 90 | | 100 | | 290 | +------------------------+ 3 rows in set (0.00 sec)
- Related Articles
- Get the minimum and maximum value from a VARCHAR column and display the result in separate MySQL columns?
- Concatenate columns from different tables in MySQL
- How MySQL stored GENERATED COLUMNS are different from MySQL virtual GENERATED COLUMNS?
- Get the minimum value from a list with multiple columns in MySQL?
- How to get the maximum value from strings with integers in MySQL?
- Get the time difference between values in different columns with MySQL
- Return maximum value from records in MySQL
- MySQL order from a different starting value?
- Get the highest score value from a single column and the greatest from two columns in MySQL
- Fetch the maximum value from a MySQL column?
- Python Pandas - Get the maximum value from Ordered CategoricalIndex
- Get rows that have common value from the same table with different id in MySQL
- How to calculate value from multiple columns in MySQL?
- Fetch maximum value from multiple columns with null and non-null values?
- Find maximum value from a VARCHAR column in MySQL

Advertisements