- 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
Get count of values that only appear once in a MySQL column?
To get number of values that only appear once in a column, use GROUP BY HAVING. Let us first create a table:
mysql> create table DemoTable ( Name varchar(20) ); Query OK, 0 rows affected (0.55 sec)
Following is the query to insert some records in the table using insert command:
mysql> insert into DemoTable values('Larry'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Larry'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Larry'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Carol'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.27 sec)
Following is the query to display records from the table using select command:
mysql> select *from DemoTable;
This will produce the following output
+--------+ | Name | +--------+ | Larry | | Larry | | Sam | | Chris | | Sam | | Mike | | Sam | | Larry | | Mike | | Mike | | Carol | | Robert | +--------+ 12 rows in set (0.00 sec)
Following is the query to count the values that only appear once in a column i.e. the count of the names that appears only once in the “Name” column:
mysql> SELECT COUNT(Name) FROM ( SELECT Name FROM DemoTable GROUP BY Name HAVING COUNT(*) = 1 ) AS APPEAR_FIRST_TIME;
This will produce the following output
+-------------+ | COUNT(Name) | +-------------+ | 3 | +-------------+ 1 row in set (0.00 sec)
- Related Articles
- Find and display duplicate values only once from a column in MySQL
- How to order return duplicate column values only once in MySQL?
- Get the count of only unique rows in a MySQL column?
- Get the count of duplicate values from a single column in MySQL?
- Get the maximum count of distinct values in a separate column with MySQL
- Can I get the count of repeated values in a column with MySQL?
- How can we get only unique values of a column in MySQL result set?
- Get multiple count in a single MySQL query for specific column values
- MySQL query to get the count of rows in which two or more specified values appear?\n
- Array elements that appear more than once?
- Select a value from MySQL database only if it exists only once from a column with duplicate and non-duplicate values
- MySQL to get only the floating-point numbers from a list of values in a column
- MySQL query to return the count of only NO values from corresponding column value
- Array elements that appear more than once in C?
- Filtering out the non-unique value to appear only once in JavaScript

Advertisements