Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Find Average and Maximum Average of Duplicate IDs



For this, use AVG(). To find the maximum average value, use MAX() and group by id. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> PlayerId int,
   -> PlayerScore int
   -> );
Query OK, 0 rows affected (0.55 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(1,78);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values(2,82);
Query OK, 1 row affected (0.25 sec)
mysql> insert into DemoTable values(1,45);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values(3,97);
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable values(2,79);
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------+-------------+
| PlayerId | PlayerScore |
+----------+-------------+
|       1 |           78 |
|       2 |           82 |
|       1 |           45 |
|       3 |           97 |
|       2 |           79 |
+----------+-------------+
5 rows in set (0.00 sec)

Here is the query to find the maximum average value for duplicate ids in MySQL −

mysql> select PlayerId from DemoTable
   -> group by PlayerId
   -> having avg(PlayerScore) > 80;

This will produce the following output −

+----------+
| PlayerId |
+----------+
|        2 |
|        3 |
+----------+
2 rows in set (0.00 sec)
Updated on: 2019-12-12T05:28:16+05:30

120 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements