- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Averaging a total from a Score column in MySQL with the count of distinct ids?
You can use DISTINCT along with COUNT(). Let us first create a table −
mysql> create table DemoTable -> ( -> Id int, -> Score int -> ); Query OK, 0 rows affected (0.95 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10,90); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(10,190); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(11,230); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(11,130); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------+-------+ | Id | Score | +------+-------+ | 10 | 90 | | 10 | 190 | | 11 | 230 | | 11 | 130 | +------+-------+ 4 rows in set (0.00 sec)
Here is the query for averaging a total from a Score column in MySQL with the count of distinct ids. The distinct ids are 2 here, therefore to find the average the total 640 i.e. 90 + 190 + 230 + 130 would be divided with 2 −
mysql> SELECT SUM(Score) / COUNT(DISTINCT Id) from DemoTable;
This will produce the following output −
+---------------------------------+ | SUM(Score) / COUNT(DISTINCT Id) | +---------------------------------+ | 320.0000 | +---------------------------------+ 1 row in set (0.00 sec)
- Related Articles
- Count duplicate ids and display the result in a separate column with MySQL
- MySQL query to display the count of distinct records from a column with duplicate records
- Get the maximum count of distinct values in a separate column with MySQL
- MySQL query to get the count of distinct records in a column
- How to count the distinct column in MySQL?
- Display distinct dates in MySQL from a column with date records
- Get minimum value from a column (floating values) with corresponding duplicate ids in MySQL
- How to get the count of each distinct value in a column in MySQL?
- How do we count the total duplicate records in a column of MySQL table?
- MySQL select distinct dates from datetime column in a table?
- MySQL query to count occurrences of distinct values and display the result in a new column?
- Select distinct values from three columns and display in a single column with MySQL
- Get the count of duplicate values from a single column in MySQL?
- MySQL query to count all the column values from two columns and exclude NULL values in the total count?
- Get the highest score value from a single column and the greatest from two columns in MySQL

Advertisements