Is there a MAX function for rows and not for columns in MySQL?


Yes, you can use GREATEST() from MySQL to check maximum from rows (not columns). Let us first create a table −

mysql> create table DemoTable
   (
   Value1 int,
   Value2 int,
   Value3 int
   );
Query OK, 0 rows affected (0.58 sec)

Insert records in the table using insert command −

mysql> insert into DemoTable values(190,395,322);
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output−

+--------+--------+--------+
| Value1 | Value2 | Value3 |
+--------+--------+--------+
| 190    | 395    | 322    |
+--------+--------+--------+
1 row in set (0.00 sec)

Here is the query to get the MAX for rows (not columns)−

mysql> select greatest(Value1,Value2,Value3) as GreaterValue from DemoTable;

This will produce the following output −

+--------------+
| GreaterValue |
+--------------+
| 395          |
+--------------+
1 row in set (0.04 sec)

Updated on: 30-Jul-2019

62 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements