
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to select average from distinct column of table?
For getting average, use AVG() and use it with DISTINCT to calculate from distinct records. Let us first create a table −
mysql> create table DemoTable1934 ( StudentName varchar(20), StudentMarks int ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1934 values('Chris',56); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1934 values('Chris',56); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1934 values('David',78); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1934 values('David',78); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1934 values('Carol',45); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1934;
This will produce the following output −
+-------------+--------------+ | StudentName | StudentMarks | +-------------+--------------+ | Chris | 56 | | Chris | 56 | | David | 78 | | David | 78 | | Carol | 45 | +-------------+--------------+ 5 rows in set (0.00 sec)
Here is the query to select average from distinct column of table −
mysql> select avg(tbl.StudentMarks) from ( select distinct StudentName,StudentMarks from DemoTable1934 ) as tbl;
This will produce the following output −
+-----------------------+ | avg(tbl.StudentMarks) | +-----------------------+ | 59.6667 | +-----------------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL select distinct dates from datetime column in a table?
- How to select distinct value from one MySQL column only?
- MySQL query to select distinct order by id
- MySQL select query to select rows from a table that are not in another table?
- How to select all distinct filename extensions from a table of filenames in MySQL?
- MySQL query to select all the records only from a specific column of a table with multiple columns
- Select the table name as a column in a UNION select query with MySQL?
- Randomly SELECT distinct rows in a MySQL table?
- MySQL query to display the count of distinct records from a column with duplicate records
- Calculate average of numbers in a column MySQL query?
- How to add a column from a select query but the value from the new column will be the row count of the MySQL select query?
- How to get a specific column record from SELECT query in MySQL?
- MYSQL select DISTINCT values from two columns?
- MySQL query to get the count of distinct records in a column
- MongoDB query to select distinct and count?

Advertisements