
- 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
Find average of corresponding records (Product Price) from duplicate product ids in MYSQL
For this, use AVG() for average and GROUP BY to group records of duplicate column (Product Id). Let us first create a table −
mysql> create table DemoTable1490 -> ( -> ProductId varchar(20), -> ProductPrice int -> ); Query OK, 0 rows affected (0.43 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1490 values('PRODUCT_100',700); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1490 values('PRODUCT_200',500); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable1490 values('PRODUCT_200',1000); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1490 values('PRODUCT_100',1300); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1490 values('PRODUCT_200',300); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1490;
This will produce the following output −
+-------------+--------------+ | ProductId | ProductPrice | +-------------+--------------+ | PRODUCT_100 | 700 | | PRODUCT_200 | 500 | | PRODUCT_200 | 1000 | | PRODUCT_100 | 1300 | | PRODUCT_200 | 300 | +-------------+--------------+ 5 rows in set (0.00 sec)
Here is the query to find average −
mysql> select ProductId,avg(ProductPrice) from DemoTable1490 -> group by ProductId;
This will produce the following output −
+-------------+-------------------+ | ProductId | avg(ProductPrice) | +-------------+-------------------+ | PRODUCT_100 | 1000.0000 | | PRODUCT_200 | 600.0000 | +-------------+-------------------+ 2 rows in set (0.03 sec)
- Related Articles
- Display highest amount from corresponding duplicate ids in MySQL
- MongoDB aggregation to sum product price with similar IDs
- SUM corresponding duplicate records in MySQL
- MySQL rows concatenation to fetch maximum corresponding value from duplicate IDs?
- MySQL SELECT products WHERE 'average price per product' < value?
- Find average on the basis of corresponding duplicate VARCHAR values in MySQL
- Add records from corresponding duplicate values in another column with MySQL
- Get minimum value from a column (floating values) with corresponding duplicate ids in MySQL
- Finding the average and display the maximum average of duplicate ids?
- Select count of values (Yes, No) with same ids but different corresponding records in MySQL?
- Find and display duplicate records in MySQL?
- Update table with duplicate ids in MySQL
- Total, Average and Marginal Product
- How to get a rating average in MongoDB based on duplicate ids?
- MySQL query to concatenate records with similar corresponding ids in a single row separated by a special character

Advertisements