- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to sum varchar column and display the count in MySQL?
For this, use GROUP BY along with COUNT(*). Let us first create a table −
mysql> create table DemoTable ( EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY, EmployeeGender varchar(40) ); Query OK, 0 rows affected (0.48 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(EmployeeGender) values('MALE'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(EmployeeGender) values('FEMALE'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(EmployeeGender) values('FEMALE'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(EmployeeGender) values('FEMALE'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(EmployeeGender) values('MALE'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable(EmployeeGender) values('MALE'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(EmployeeGender) values('MALE'); Query OK, 1 row affected (0.07 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------------+----------------+ | EmployeeId | EmployeeGender | +------------+----------------+ | 1 | MALE | | 2 | FEMALE | | 3 | FEMALE | | 4 | FEMALE | | 5 | MALE | | 6 | MALE | | 7 | MALE | +------------+----------------+ 7 rows in set (0.00 sec)
Following is the query to sum the varchar column. This will sum the MALE and FEMALE column values and count would be displayed −
mysql> select EmployeeGender,count(*) from DemoTable group by EmployeeGender;
This will produce the following output −
+----------------+----------+ | EmployeeGender | count(*) | +----------------+----------+ | MALE | 4 | | FEMALE | 3 | +----------------+----------+ 2 rows in set (0.00 sec)
- Related Articles
- Update all varchar column rows to display values before slash in MySQL?
- How to alter a MySQL Column from varchar(30) to varchar(100)?
- Get the minimum and maximum value from a VARCHAR column and display the result in separate MySQL columns?
- MySQL query to group by names and display the count in a new column
- MySQL query to group by column and display the sum of similar values in another column
- How to find repeated rows and display there count in a separate column with MySQL?
- Count duplicate ids and display the result in a separate column with MySQL
- MySQL query to count the duplicate ID values and display the result in a separate column
- How to count the distinct column in MySQL?
- MySQL query to count occurrences of distinct values and display the result in a new column?
- Fetch the size of a specific column values in MySQL and display the sum
- MySQL query to get the count of values and display the count in a new column ordered in descending order\n
- Display the count of duplicate records from a column in MySQL and order the result
- How to select only non - numeric values from varchar column in MySQL?
- How to search on a MySQL varchar column using a number?

Advertisements