Find the greatest value among four tables in MySQL?


To find the greatest value among four tables, you can use the method GREATEST(). Following is the query to create first table −

<DemoTable1>

mysql> create table DemoTable1
   -> (
   -> Value int
   -> );
Query OK, 0 rows affected (0.70 sec)

Insert some records in the first table using insert command −

mysql> insert into DemoTable1 values(10);
Query OK, 1 row affected (0.10 sec)

mysql> insert into DemoTable1 values(60);
Query OK, 1 row affected (0.14 sec)

Display all records from the first table using select statement −

mysql> select *from DemoTable1;

Output

+-------+
| Value |
+-------+
| 10    |
| 60    |
+-------+
2 rows in set (0.00 sec)

Following is the query to create second table −

<DemoTable2>

mysql> create table DemoTable2
   -> (
   -> Value int
   -> );
Query OK, 0 rows affected (0.67 sec)

Insert some records in the second table using insert command −

mysql> insert into DemoTable2 values(90);
Query OK, 1 row affected (0.15 sec)

mysql> insert into DemoTable2 values(12);;
Query OK, 1 row affected (0.12 sec)

Display all records from the second table using select statement −

mysql> select *from DemoTable2;

Output

+-------+
| Value |
+-------+
| 90    |
| 12    |
+-------+
2 rows in set (0.00 sec)

Following is the query to create third table −

<DemoTable3>

mysql> create table DemoTable3
   -> (
   -> Value int
   -> );
Query OK, 0 rows affected (0.65 sec)

Insert some records in the third table using insert command −

mysql> insert into DemoTable3 values(34);
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable3 values(120);
Query OK, 1 row affected (0.17 sec)

Display all records from the third table using select statement −

mysql> select *from DemoTable3;

Output

+-------+
| Value |
+-------+
| 34    |
| 120   |
+-------+
2 rows in set (0.00 sec)

Following is the query to create fourth table −

<DemoTable4>

mysql> create table DemoTable4
   -> (
   -> Value int
   -> );
Query OK, 0 rows affected (0.68 sec)

Insert some records in the fourth table using insert command −

mysql> insert into DemoTable4 values(140);
Query OK, 1 row affected (0.72 sec)

mysql> insert into DemoTable4 values(290);
Query OK, 1 row affected (0.14 sec)

Display all records from the fourth table using select statement −

mysql> select *from DemoTable4;

output

+-------+
| Value |
+-------+
| 140   |
| 290   |
+-------+
2 rows in set (0.00 sec)

Here is the query to find the greatest value among four tables in MySQL −

mysql> select greatest(
   -> (select max(Value) from DemoTable1),
   -> (select max(Value) from DemoTable2),
   -> (select max(Value) from DemoTable3),
   -> (select max(Value) from DemoTable4)
   -> ) as MaximumValue;

Output

+--------------+
| MaximumValue |
+--------------+
| 290          |
+--------------+
1 row in set (0.00 sec)

Updated on: 30-Jul-2019

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements