Get the highest score value from a single column and the greatest from two columns in MySQL


Let us first create a table −

mysql> create table DemoTable790 (
   Score1 int,
   Score2 int
);
Query OK, 0 rows affected (0.45 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable790 values(98,76);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable790 values(78,89);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable790 values(85,68);
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable790;

This will produce the following output -

+--------+--------+
| Score1 | Score2 |
+--------+--------+
| 98     | 76     |
| 78     | 89     |
| 85     | 68     |
+--------+--------+
3 rows in set (0.00 sec)

The max() is as follows to get the maximum value from a single column −

mysql> select max(Score1) from DemoTable790;

This will produce the following output -

+-------------+
| max(Score1) |
+-------------+
| 98          |
+-------------+
1 row in set (0.00 sec)

The greatest() is as follows to get the greatest value from two columns −

mysql> select greatest(Score1,Score2) from DemoTable790;

This will produce the following output -

+-------------------------+
| greatest(Score1,Score2) |
+-------------------------+
| 98                      |
| 89                      |
| 85                      |
+-------------------------+
3 rows in set (0.03 sec)
raja
Published on 09-Sep-2019 12:11:28
Advertisements