
- 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
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 Articles
- Perform case insensitive SELECT using MySQL IN()?
- Perform multiple counting without using MySQL COUNT()?
- Perform MySQL SELECT INTO user-defined variable
- MySQL select count by value?
- MySQL SELECT DISTINCT and count?
- Perform count with CASE WHEN statement in MySQL?
- Perform MySQL SELECT on fields containing null values?
- 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
- MySQL query to select a count on two separate conditions?
- How to call a stored procedure using select statement in MySQL?
- How to select the top two values using LIMIT in MySQL?
- How to perform Increment in MySQL Update?
- How to perform string matching in MySQL?

Advertisements