

- 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
How to determine if a value appears in a GROUP BY group in MySQL?
You can use aggregate function SUM() along with IF to determine if a value appears in a GROUP BY group.
Let us first create a demo table
mysql> create table GroupbygroupDemo -> ( -> UserId int, -> UserName varchar(20) -> ); Query OK, 0 rows affected (1.48 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into GroupbygroupDemo values(10,'John'); Query OK, 1 row affected (0.14 sec) mysql> insert into GroupbygroupDemo values(10,'Carol'); Query OK, 1 row affected (0.08 sec) mysql> insert into GroupbygroupDemo values(10,'Carol'); Query OK, 1 row affected (0.12 sec) mysql> insert into GroupbygroupDemo values(20,'David'); Query OK, 1 row affected (0.17 sec) mysql> insert into GroupbygroupDemo values(30,'John'); Query OK, 1 row affected (0.12 sec) mysql> insert into GroupbygroupDemo values(30,'David'); Query OK, 1 row affected (0.20 sec) mysql> insert into GroupbygroupDemo values(30,'Mike'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from GroupbygroupDemo;
The output is as follows
+--------+----------+ | UserId | UserName | +--------+----------+ | 10 | John | | 10 | Carol | | 10 | Carol | | 20 | David | | 30 | John | | 30 | David | | 30 | Mike | +--------+----------+ 7 rows in set (0.00 sec)
Here is the query to determine if a value appears in a GROUP BY group
mysql> select UserId, -> if(sum(UserName='David'),'YES','NO') as Correct_Name_David -> from GroupbygroupDemo -> group by UserId;
The following is the output
+--------+--------------------+ | UserId | Correct_Name_David | +--------+--------------------+ | 10 | NO | | 20 | YES | | 30 | YES | +--------+--------------------+ 3 rows in set (0.08 sec)
- Related Questions & Answers
- Listing all rows by group with MySQL GROUP BY?
- GROUP BY the number of rows returned by GROUP BY in MySQL?
- GROUP BY a column in another MySQL table
- Group MySQL rows in an array by column value?
- Using GROUP BY and COUNT in a single MySQL query to group duplicate records and display corresponding max value
- How to implement GROUP by range in MySQL?
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- HAVING with GROUP BY in MySQL
- Check If every group of a is followed by a group of b of same length in Python
- Group by dates in a MongoDB collection?
- How to perform conditional GROUP BY in MySQL to fetch?
- SELECT DISTINCT vs GROUP BY in MySQL?
- Get rows with GROUP BY in MySQL?
- Getting last value in MySQL group concat?
- MySQL query to GROUP BY multiple columns
Advertisements