
- 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 for grouping and summing the values based on specific records
Use GROUP BY to group records, whereas SUM() function is used to add. Let us first create a table −
mysql> create table DemoTable ( Name varchar(40), Subject varchar(40), Marks int ); Query OK, 0 rows affected (2.89 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris','MySQL',76); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values('Sam','MongoDB',86); Query OK, 1 row affected (0.39 sec) mysql> insert into DemoTable values('Mike','MySQL',98); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable values('David','Java',93); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values('Bob','MySQL',57); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('John','MongoDB',77); 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 −
+-------+---------+-------+ | Name | Subject | Marks | +-------+---------+-------+ | Chris | MySQL | 76 | | Sam | MongoDB | 86 | | Mike | MySQL | 98 | | David | Java | 93 | | Bob | MySQL | 57 | | John | MongoDB | 77 | +-------+---------+-------+ 6 rows in set (0.00 sec)
Following is the query to group and sum the values on the basis of SUBJECT −
mysql> select Subject,SUM(Marks) from DemoTable group by Subject;
This will produce the following output −
+---------+------------+ | Subject | SUM(Marks) | +---------+------------+ | MySQL | 231 | | MongoDB | 163 | | Java | 93 | +---------+------------+ 3 rows in set (0.00 sec)
- Related Articles
- How to sum selected column values based on specific month records in MySQL?
- MySQL to fetch records based on a specific month and year?
- MySQL db query to fetch records from comma separate values on the basis of a specific value
- MySQL query to fetch specific records matched from an array (comma separated values)
- MySQL SELECT query to return records with specific month and year
- Replace records based on conditions in MySQL?
- MySQL query to order records but fix a specific name and display rest of the values (only some) random
- Count the occurrences of specific records (duplicate) in one MySQL query
- MySQL query to count records that begin with specific letters
- MySQL query to select records beginning from a specific id
- Get multiple count in a single MySQL query for specific column values
- Display records by grouping dates in MySQL
- ORDER BY records in MySQL based on a condition
- MySQL query to concatenate all the values in each row based on the common matching ID
- MongoDB Query to search for records only in a specific hour?

Advertisements