
- 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 find the average of rows with the same ID
Let us first create a table −
mysql> create table DemoTable ( StudentId int, StudentMarks int ); Query OK, 0 rows affected (0.83 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(1000,78); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(1001,88); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(1000,89); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(1000,67); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(1000,90); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(1001,91); 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 −
+-----------+--------------+ | StudentId | StudentMarks | +-----------+--------------+ | 1000 | 78 | | 1001 | 88 | | 1000 | 89 | | 1000 | 67 | | 1000 | 90 | | 1001 | 91 | +-----------+--------------+ 6 rows in set (0.00 sec)
Following is the query to find the average of rows with the same ID −
mysql> select StudentId,avg(StudentMarks) from DemoTable group by StudentId;
This will produce the following output −
+-----------+-------------------+ | StudentId | avg(StudentMarks) | +-----------+-------------------+ | 1000 | 81.0000 | | 1001 | 89.5000 | +-----------+-------------------+ 2 rows in set (0.03 sec)
- Related Articles
- MySQL - SUM rows with same ID?
- MySQL query to find all rows where ID is divisible by 4?
- MySQL query to sum rows having repeated corresponding Id
- MySQL query to merge rows if Id is the same and display the highest corresponding value from other columns
- MySQL query to find the number of rows in the last query
- Get rows that have common value from the same table with different id in MySQL
- MySQL query to delete a record with the lowest ID?
- MySQL query to find sum of fields with same column value?
- MySQL query to count frequency of students with the same age?
- MySQL query to find the average of only first three values from a column with five values
- Shifting values of rows in MySQL to change the existing id values for existing rows?
- How to increment all the rows of a particular column by 1 in a single MySQL query (ID column +1)?
- MySQL query to compare and display only the rows with NULL values
- How to insert multiple rows with single MySQL query?
- MySQL query to count rows with a specific column?

Advertisements