Getting maximum from a column value and set it for all other values in the same column with MySQL?

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable
(
   FirstName varchar(100),
   Score int
);
Query OK, 0 rows affected (0.60 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('David',59);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('Chris',97);
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable values('Bob',98);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('Carol',91);
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-----------+-------+
| FirstName | Score |
+-----------+-------+
| David     |    59 |
| Chris     |    97 |
| Bob       |    98 |
| Carol     |    91 |
+-----------+-------+
4 rows in set (0.00 sec)

Following is the query to get maximum from a column value and set it for all other values in the same column −

mysql> select FirstName,(select max(Score) from DemoTable) as Score from DemoTable;

This will produce the following output −

+-----------+-------+
| FirstName | Score |
+-----------+-------+
| David     |    98 |
| Chris     |    98 |
| Bob       |    98 |
| Carol     |    98 |
+-----------+-------+
4 rows in set (0.00 sec)
raja
Published on 27-Sep-2019 06:30:20
Advertisements