Return maximum value from records in MySQL


Let us first create a table −

mysql> create table DemoTable1449
   -> (
   -> PlayerId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> PlayerScore int
   -> );
Query OK, 0 rows affected (0.69 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1449(PlayerScore) values(1040);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1449(PlayerScore) values(1450);
Query OK, 1 row affected (0.34 sec)
mysql> insert into DemoTable1449(PlayerScore) values(1890);
Query OK, 1 row affected (0.72 sec)
mysql> insert into DemoTable1449(PlayerScore) values(1650);
Query OK, 1 row affected (0.25 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1449;

This will produce the following output −

+----------+-------------+
| PlayerId | PlayerScore |
+----------+-------------+
|        1 |        1040 |
|        2 |        1450 |
|        3 |        1890 |
|        4 |        1650 |
+----------+-------------+
4 rows in set (0.00 sec)

Following is the query to return maximum value from records −

mysql> select PlayerId,PlayerScore from DemoTable1449
   -> where PlayerScore=( select max(PlayerScore) PlayerScore from DemoTable1449);

This will produce the following output −

+----------+-------------+
| PlayerId | PlayerScore |
+----------+-------------+
|        3 |        1890 |
+----------+-------------+
1 row in set (0.00 sec)

Updated on: 10-Dec-2019

132 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements