
- 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
MySQL query to count the duplicate ID values and display the result in a separate column
To count the duplicate ID values, use aggregate function COUNT() and GROUP BY. Let us first create a table −
mysql> create table DemoTable ( Id int, Name varchar(100) ); Query OK, 0 rows affected (1.30 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(50,'Chris'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(51,'David'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(51,'Mike'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(50,'Sam'); 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 −
+------+-------+ | Id | Name | +------+-------+ | 50 | Chris | | 51 | David | | 51 | Mike | | 50 | Sam | +------+-------+ 4 rows in set (0.00 sec)
Following is the query to display the count of duplicate ID values in a new column −
mysql> select Id,count(Name) as Count from DemoTable group by Id;
This will produce the following output −
+------+-------+ | Id | Count | +------+-------+ | 50 | 2 | | 51 | 2 | +------+-------+ 2 rows in set (0.00 sec)
- Related Questions & Answers
- Count duplicate ids and display the result in a separate column with MySQL
- MySQL query to count occurrences of distinct values and display the result in a new column?
- Display the count of duplicate records from a column in MySQL and order the result
- MySQL query to find duplicate tuples and display the count?
- MySQL query to group results by date and display the count of duplicate values?
- Concatenate the column values with separate text in MySQL and display in a single column
- MySQL query to count number of duplicate values in a table column
- MySQL query to display the first alphabet from strings in a separate column
- MySQL query to display the count of distinct records from a column with duplicate records
- Get the minimum and maximum value from a VARCHAR column and display the result in separate MySQL columns?
- Query to divide the values of two columns and display the result in a new column using MySQL wildcard?
- Setting column values as column names in the MySQL query result?
- MySQL query to get the count of values and display the count in a new column ordered in descending order
- How to concatenate columns based on corresponding duplicate id values in MySQL? Display the duplicate values in the same column separated by slash
- How to round MySQL column with float values and display the result in a new column?
Advertisements