
- 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
Get the sum of columns for duplicate records in MySQL
For this, use GROUP BY clause along with aggregate function SUM(). Let us first create a table −
mysql> create table DemoTable( Name varchar(100), Score int ); Query OK, 0 rows affected (0.70 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Adam',50); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Bob',80); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Adam',70); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Adam',10); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Carol',98); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Bob',10); Query OK, 1 row affected (0.08 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+-------+ | Name | Score | +-------+-------+ | Adam | 50 | | Bob | 80 | | Adam | 70 | | Adam | 10 | | Carol | 98 | | Bob | 10 | +-------+-------+ 6 rows in set (0.00 sec)
Following is the query to get the sum of columns for duplicate records in MySQL −
mysql> select Name,sum(Score) from DemoTable group by Name;
This will produce the following output −
+-------+------------+ | Name | sum(Score) | +-------+------------+ | Adam | 130 | | Bob | 90 | | Carol | 98 | +-------+------------+ 3 rows in set (0.00 sec)
- Related Articles
- SUM corresponding duplicate records in MySQL
- Get the SUM of records between two given dates in MySQL
- Which technique is more efficient for replacing duplicate records in MySQL?
- Find and display duplicate records in MySQL?
- Count the occurrences of specific records (duplicate) in one MySQL query
- Implement MySQL IN for 2 columns to display only selected records
- MySQL query to get the current date records wherein one of the columns displays current date
- How to get duplicate records from Android sqlite?
- MySQL query to display the count of distinct records from a column with duplicate records
- How to delete all the duplicate records in a MySQL table?
- How do we count the total duplicate records in a column of MySQL table?
- Get the number of columns in a MySQL table?
- Display the count of duplicate records from a column in MySQL and order the result
- Find average of corresponding records (Product Price) from duplicate product ids in MYSQL
- Get count of zeros for columns values declared with INT type in MySQL

Advertisements