
- 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
MySQL query to avoid displaying duplicates values?
For this, you can use GROUP BY and use COUNT to get only non-duplicate values. Following is the syntax −
select yourColumnName from yourTableName group by yourColumnName having count(*)=1;
Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Subject varchar(100) -> ); Query OK, 0 rows affected (0.70 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Subject) values('MySQL'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(Subject) values('MongoDB'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Subject) values('Java'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Subject) values('MongoDB'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Subject) values('C'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Subject) values('C++'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Subject) values('Java'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Subject) values('C'); Query OK, 1 row affected (0.23 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+----+---------+ | Id | Subject | +----+---------+ | 1 | MySQL | | 2 | MongoDB | | 3 | Java | | 4 | MongoDB | | 5 | C | | 6 | C++ | | 7 | Java | | 8 | C | +----+---------+ 8 rows in set (0.00 sec)
Here is the query to avoid displaying duplicates −
mysql> select Subject from DemoTable group by Subject having count(*)=1;
Output
+---------+ | Subject | +---------+ | MySQL | | C++ | +---------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL query to alphabetize records and count the duplicates?
- MySQL query to sum 3 different values in a column displaying total of each value in result set?
- MySQL UNIQUE declaration to avoid inserting duplicate values?
- How to add duplicate varchar values without displaying error in MySQL?
- How to write a single MySQL query for displaying a value for multiple inputs?
- MySQL query to order by NULL values
- MySQL query to fetch multiple least values?
- MySQL query to convert empty values to NULL?
- Select query using MySQL IN() and avoid sorting in it
- MySQL query to get the dates between range of records displaying student’s Date of Birth?
- MySQL query to change a string by displaying only the part of string after underscore?
- MySQL Query to get count of unique values?
- MySQL query to set values for NULL occurrence
- MySQL Query a List of Values?
- MySQL query to remove special characters from column values?

Advertisements