
- 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 group by column and display the sum of similar values in another column
For this, use GROUP BY HAVING clause.
Let us first create a table −
mysql> create table DemoTable782 ( Name varchar(100), Score int ); Query OK, 0 rows affected (1.18 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable782 values('John',156); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable782 values('Carol',250); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable782 values('Bob',140); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable782 values('John',126); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable782 values('John',140); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable782 values('Bob',280); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable782 values('Bob',250); Query OK, 1 row affected (0.41 sec) mysql> insert into DemoTable782 values('Carol',189); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable782 values('Carol',299); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable782;
This will produce the following output -
+-------+-------+ | Name | Score | +-------+-------+ | John | 156 | | Carol | 250 | | Bob | 140 | | John | 126 | | John | 140 | | Bob | 280 | | Bob | 250 | | Carol | 189 | | Carol | 299 | +-------+-------+ 9 rows in set (0.00 sec)
Following is the query to group by column and display the sum of similar values −
mysql> select Name,SUM(Score) AS Total from DemoTable782 group by Name HAVING Total > 500;
This will produce the following output -
+-------+-------+ | Name | Total | +-------+-------+ | Carol | 738 | | Bob | 670 | +-------+-------+ 2 rows in set (0.00 sec)
- Related Articles
- GROUP BY and display only non-empty column values in MySQL
- MySQL query to group by names and display the count in a new column
- Group by one column and display corresponding records from another column with a separator in MySQL
- MySQL query to select rows where column value is only 0, group by another column?
- MySQL query to sum the Product Price values from similar columns for same customers and display the result in the same column
- MySQL query to display record with maximum count values in a group with other column values?
- MySQL query to display only the column values with corresponding column having whitespace
- GROUP BY a column in another MySQL table
- MySQL query to display the column and its values using OR in WHERE statement
- MySQL query to group results by date and display the count of duplicate values?
- Fetch the size of a specific column values in MySQL and display the sum
- MySQL query to count occurrences of distinct values and display the result in a new column?
- MySQL query to get sum of each column where every column has same number of values?
- MySQL query to count the duplicate ID values and display the result in a separate column
- MySQL query to get string from one column and find its position in another column with comma separated values?

Advertisements