
- 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
Implement MySQL conditional GROUP BY with NOT IN to filter records from duplicate column values
Let us first create a table −
mysql> create table DemoTable ( Name varchar(40), Score int ); Query OK, 0 rows affected (0.48 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Adam',89); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Adam',89); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Chris',89); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Chris',89); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Bob',98); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Bob',89); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+-------+ | Name | Score | +-------+-------+ | Adam | 89 | | Adam | 89 | | Chris | 89 | | Chris | 89 | | Bob | 98 | | Bob | 89 | +-------+-------+ 6 rows in set (0.00 sec)
Following is the query to achieve conditional GROUP BY using NOT IN() −
mysql> select distinct Name from DemoTable where Name not in(select Name from DemoTable where Score <> 89);
This will produce the following output −
+-------+ | Name | +-------+ | Adam | | Chris | +-------+q 2 rows in set (0.06 sec)
- Related Articles
- Add records from corresponding duplicate values in another column with MySQL
- Get first date from timestamp in MySQL group by another column with duplicate value
- Group by one column and display corresponding records from another column with a separator in MySQL
- MySQL query to display the count of distinct records from a column with duplicate records
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Remove/ filter duplicate records from array - JavaScript?
- MySQL group by for separate id without using GROUP BY to remove duplicate column row?
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- Find specific records from a column with comma separated values in MySQL
- Fetch specific rows from a MySQL table with duplicate column values (names)?
- How to perform conditional GROUP BY in MySQL to fetch?
- Using GROUP BY and COUNT in a single MySQL query to group duplicate records and display corresponding max value
- Get minimum value from a column (floating values) with corresponding duplicate ids in MySQL
- How to implement GROUP by range in MySQL?
- MySQL query to fetch the maximum corresponding value from duplicate column values

Advertisements