

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL query to find the top two highest scores
For this, use aggregate function MAX(). Let us first create a table −
mysql> create table DemoTable1383 -> ( -> Id int, -> PlayerScore int -> ); Query OK, 0 rows affected (0.90 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1383 values(200,78); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1383 values(200,89); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1383 values(200,89); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1383 values(200,87); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable1383 values(203,97); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1383 values(200,57); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1383;
This will produce the following output −
+------+-------------+ | Id | PlayerScore | +------+-------------+ | 200 | 78 | | 200 | 89 | | 200 | 89 | | 200 | 87 | | 203 | 97 | | 200 | 57 | +------+-------------+ 6 rows in set (0.00 sec)
Following is the query to find highest score and display distinct value −
mysql> select Id,max(PlayerScore) from DemoTable1383 group by Id order by max(PlayerScore) DESC;
This will produce the following output −
+------+------------------+ | Id | max(PlayerScore) | +------+------------------+ | 203 | 97 | | 200 | 89 | +------+------------------+ 2 rows in set (0.00 sec)
- Related Questions & Answers
- A single MySQL query to find the highest and lowest among two tables?
- MySQL query to get first two highest column values from a table?
- How to get the fourth highest value using MySQL query?
- MongoDB query to find the highest numeric value of a column?
- MySQL query to select top 10 records?
- Query MySQL database to echo highest auto incremented number?
- MySQL query to find the number of occurrences from two columns?
- MySQL query to place a specific record on the top
- MySQL query to select top n rows efficiently?
- How to order by the highest value from two columns in MySQL?
- How to select the top two values using LIMIT in MySQL?
- MySQL query to get the highest value from a single row with multiple columns
- MySQL query to select the nth highest value in a column by skipping values
- Write the DB2 SQL query to find the third highest ORDER_TOTAL in a ORDERS DB2 table
- How to find nth highest value of a MySQL column?
Advertisements