

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 perform SELECT using COUNT in MySQL?
To perform SELECT with COUNT, use aggregate function COUNT(). Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(100), Subject varchar(100) ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Name,Subject) values('John','MySQL'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Name,Subject) values('John','Java'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Name,Subject) values('Carol','MongoDB'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Name,Subject) values('Carol','Java'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Name,Subject) values('Carol','MySQL'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Name,Subject) values('John','MySQL'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(Name,Subject) values('Carol','MongoDB'); Query OK, 1 row affected (0.26 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+----+-------+---------+ | Id | Name | Subject | +----+-------+---------+ | 1 | John | MySQL | | 2 | John | Java | | 3 | Carol | MongoDB | | 4 | Carol | Java | | 5 | Carol | MySQL | | 6 | John | MySQL | | 7 | Carol | MongoDB | +----+-------+---------+ 7 rows in set (0.00 sec)
Following is the query to select using count in MySQL −
mysql> select Name,Subject,count(Subject) from DemoTable group by Subject,Name;
Output
+-------+---------+----------------+ | Name | Subject | count(Subject) | +-------+---------+----------------+ | John | MySQL | 2 | | John | Java | 1 | | Carol | MongoDB | 2 | | Carol | Java | 1 | | Carol | MySQL | 1 | +-------+---------+----------------+ 5 rows in set (0.00 sec)
- Related Questions & Answers
- Perform case insensitive SELECT using MySQL IN()?
- Perform multiple counting without using MySQL COUNT()?
- Perform MySQL SELECT INTO user-defined variable
- MySQL SELECT DISTINCT and count?
- MySQL select count by value?
- Perform MySQL SELECT on fields containing null values?
- Perform count with CASE WHEN statement in MySQL?
- Perform multiplication in SELECT depending on column value in MySQL?
- MySQL SELECT from a subquery and then perform DELETE?
- Perform multiple inserts with INSERT INTO SELECT and UNION in MySQL
- How to perform Increment in MySQL Update?
- How to perform string matching in MySQL?
- MySQL query to perform selection using AND OR
- MySQL query to select a count on two separate conditions?
- Insert record using MySQL SELECT?
Advertisements